Last active
November 20, 2017 10:56
-
-
Save xavxyz/32583dcae3dbe4005eb36824574aa3b0 to your computer and use it in GitHub Desktop.
Snippet from the Advanced GraphQL workshop I run at OK GROW! - https://www.okgrow.com/graphql
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
// define a way to connect to the external GraphQL API | |
const link = new HttpLink({ | |
uri: 'https://external-api.com/graphql', | |
fetch, | |
}); | |
// create a remote schema from the external GraphQL API | |
const remoteSchema = makeRemoteExecutableSchema({ | |
schema: await introspectSchema(link), | |
link, | |
}); | |
// create the local schema | |
const localSchema = makeExecutableSchema({ typeDefs, resolvers }); | |
// from a local type, how to access a remote type | |
const linkSchema = ` | |
extend type Location { | |
weather: Weather | |
} | |
`; | |
// the schema that we'll serve | |
const schema = mergeSchemas({ | |
// all three above schemas together | |
schemas: [remoteSchema, localSchema, linkSchema], | |
resolvers: mergeInfo => ({ | |
// specific resolver for the type definition linking the local schema & the remote schema | |
Location: { | |
weather: { | |
fragment: | |
'fragment WeatherLocation on Location { longitude latitude }', | |
resolve: ({ longitude, latitude }, args, context, info) => { | |
return mergeInfo.delegate( | |
'query', | |
'weather', | |
{ | |
coords: { | |
longitude, | |
latitude, | |
}, | |
}, | |
context, | |
info | |
); | |
}, | |
}, | |
}, | |
}), | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment