Created
January 8, 2018 21:09
-
-
Save Piefayth/3ca86981b2e575067ebb47cb6a6f6385 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
const express = require('express') | |
const graphqlHTTP = require('express-graphql') | |
const { makeExecutableSchema, mergeSchemas } = require('graphql-tools') | |
const _ = require('lodash') | |
const app = express(); | |
const standaloneResolvers = { | |
Query: { | |
standalone: (data) => { | |
return { foo: "foo" } | |
} | |
}, | |
Standalone: { | |
foo: (data) => { | |
return "Standalone.foo should always return this string" | |
} | |
} | |
} | |
const dependantResolvers = { | |
Query: { | |
dependant: () => { | |
return { | |
dontNeedExternalTypeField: "baz" | |
} | |
} | |
} | |
} | |
const StandaloneSchema = makeExecutableSchema({ | |
typeDefs: ` | |
type Query { | |
standalone: Standalone | |
} | |
type Standalone { | |
foo: String | |
} | |
`, | |
resolvers: standaloneResolvers | |
}) | |
const DependantSchema = makeExecutableSchema({ | |
typeDefs: ` | |
type Query { | |
dependant: Dependant | |
} | |
type Dependant { | |
dontNeedExternalTypeField: String | |
} | |
`, | |
resolvers: dependantResolvers | |
}) | |
const linkTypeDefs = ` | |
extend type Dependant { | |
needExternalTypeField: Standalone | |
} | |
` | |
const linkResolvers = (mergeInfo) => ({ | |
Dependant: { | |
needExternalTypeField: { | |
resolve(parent, args, context, info) { | |
return mergeInfo.delegate('query', 'standalone', {}, context, info) | |
} | |
} | |
} | |
}) | |
const mergedSchema = mergeSchemas({ | |
schemas: [StandaloneSchema, DependantSchema, linkTypeDefs], | |
resolvers: linkResolvers | |
}) | |
app.use('/graphql', graphqlHTTP({ | |
schema: mergedSchema, | |
graphiql: true | |
})) | |
app.listen(4000) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment