You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This is a somewhat odd one, but caused a lot of headache before I figured out where it went wrong. Essentially, I have a GraphQL server as an api-gateway, that links to other services through the magic of makeRemoteExecutableSchema. Here's a tiny working example of the setup:
import{execute,toPromise}from'apollo-link';import{createHttpLink}from'apollo-link-http';import{ApolloServer,gql}from'apollo-server';import{buildClientSchema,IntrospectionQuery,introspectionQuery,parse,}from'graphql';import{makeExecutableSchema,makeRemoteExecutableSchema,}from'graphql-tools';importfetchfrom'node-fetch';asyncfunctionstartAPI(){// Create the link to the backend serviceconstlink=createHttpLink({uri: 'http://localhost:5001/graphql',fetch: fetchasany,});// Fetch the schema from the backend serviceconstintrospectedSchema=awaittoPromise(execute(link,{query: parse(introspectionQuery)}),).then(res=>res.dataasIntrospectionQuery);constschema=makeRemoteExecutableSchema({
link,schema: buildClientSchema(introspectedSchema),});// Start the API-server on port 5000constserver=newApolloServer({ schema });server.listen(5000).then(()=>console.log('API gateway started')).catch(console.log);}asyncfunctionstartBackendService(){// TypeDefs with a non-root typeconsttypeDefs=gql` type Product { name: String price(asOf: String): Int } type Query { products: [Product] } `;constresolvers={Query: {products: ()=>{return[{name: 'First product',price: 1234,},{name: 'Another Product',price: 18,},];},},Product: {/* Dummy resolver that return null for certain arguments */price: (source: {name: string;price: number},args?: {asOf?: string},)=>{if(args&&args.asOf){constrequestedDate=newDate(args.asOf);consttoday=newDate();if(requestedDate>today){returnnull;}}returnsource.price;},},};constserver=newApolloServer({schema: makeExecutableSchema({ typeDefs, resolvers }),});// Start the server on port 5001awaitserver.listen(5001).then(()=>console.log('Backend started')).catch(console.log);}startBackendService().then(startAPI)
Compiling and running the above code works fine and serves the playground on http://localhost:5000/ (and on 5001).
Also
# Write your query or mutation herequery{
products{
namepriceTomorrow: price(asOf: "2020-07-16")
}
}
Note however that this only applies when the aliased field is accompanied by a field with the original field name (aliased or not). For example this query:
# Write your query or mutation herequery{
products{
price: namepriceTomorrow: price(asOf: "2020-07-16")
}
}
throws an error, even though I would expect it to return
This is a somewhat odd one, but caused a lot of headache before I figured out where it went wrong. Essentially, I have a GraphQL server as an api-gateway, that links to other services through the magic of
makeRemoteExecutableSchema
. Here's a tiny working example of the setup:Compiling and running the above code works fine and serves the playground on http://localhost:5000/ (and on 5001).
Also
yields the expected
However, if we run
we get
where I would expect
This is due to the api-gateway assigning the defaultMergedResolver to the fields in Product, and the defaultMergedResolver not expecting fields to be null.
Note however that this only applies when the aliased field is accompanied by a field with the original field name (aliased or not). For example this query:
throws an error, even though I would expect it to return
but this
works perfectly fine.
The text was updated successfully, but these errors were encountered: