-
-
Save freiksenet/27ac99a19bba55fd3979b36e5f59c8ad to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { mergeSchemas, delegateToSchema } from 'graphql-tools'; | |
const extensionSchema = ` | |
interface Node { | |
id: ID! | |
} | |
extend type User implements Node { | |
id: ID! | |
} | |
extend type Blog implements Node { | |
id: ID! | |
} | |
extend type Query { | |
node(id: ID!): Node | |
} | |
`; | |
const schema = mergeSchemas({ | |
schemas: [serviceSchema, extensionSchema], | |
resolvers: { | |
Query: { | |
node(parent, { id }, context, info) { | |
const { type, localId } = deserializeGlobalId(id); | |
let fieldName; | |
if (type === 'Blog') { | |
fieldName = 'blogById', | |
} else if (type === 'User') { | |
fieldName = 'userById', | |
} else { | |
throw new Error('Invalid global id'); | |
} | |
return delegateToSchema({ | |
schema: serviceSchema, | |
operation: 'query', | |
fieldName, | |
args: { id: localId }, | |
context, | |
info, | |
}); | |
}, | |
}, | |
// ... more resolvers | |
}, | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment