2

I'm really new to Graphql (just yesterday actually). I am "playing" around and try the various tools of the ecosystem (apollo-server, graphql.js ...ect).

For the sake of experimenting, I am trying to call a query from within nodejs (and not from a client in the browser, such as a react application)

First of all this is my simple schema along with resolvers:

export const mySchema = gql`
   type User {
      id: ID!
      name:  
      surname: String
   }

   # root query has been defined in another file
   extend type Query {
      users: [User]
      test: [User]
   }
` 
export const myResolvers = {
   users: () => [ __array_of_users__ ],
   test: () => /* this is where I would like to re-invoke the 'users query'
}

Using the makeExecutableSchema function, I create a schema object with my types and my resolvers and I export this schema into the apollo server application. Every thing works fine so far.

Now following this stackoverflow suggested solution, I created a helper function which should allow me to invoke a query defined in my schema as following:

import { graphql } from "graphql";
import { schema } from "./my-schema";

export const execute = str => {
  return graphql(schema, str );
};

With this helper function, my resolvers become:

import { gql } from "apollo-server-express";
import { execute } from '__path_to_helper_function__';

export const myResolvers = {
  users: () => [ __array_of_users__ ],
  test:  () => execute( gql`
     query users {
           name
      }    
  `)
}

But in the playground, when I try the query:

   {
      test {
        name
      }
   }

I get the following error:

enter image description here

I don't even know if what I am trying to do (to call a query from within node) can be done. Any suggestion will be greatly appreciated.

Thnaks

TheSoul
  • 4,906
  • 13
  • 44
  • 74

1 Answers1

1

graphql-tag takes a string and parses it into a DocumentNode object. This is effectively the same as passing a String to the parse function. Some functions exported by the graphql module, like execute, expect to be passed in a DocumentNode object -- the graphql function does not. It should be passed just a plain String as the request, as you can see from the signature:

graphql(
  schema: GraphQLSchema,
  requestString: string,
  rootValue?: ?any,
  contextValue?: ?any,
  variableValues?: ?{[key: string]: any},
  operationName?: ?string
): Promise<GraphQLResult>

So, just drop the gql tag. You can see an (incomplete) API reference here.

Daniel Rearden
  • 80,636
  • 11
  • 185
  • 183
  • Thanks for your answer. I removed the 'gql' as suggested and pass now a string: ' query users { name } '. I now get the error: "Cannot query field "name" on type "Query"." What could this error mean? – TheSoul Jun 16 '19 at 23:38
  • You are requesting the field `name` on the root query type, which doesn't exist. Presumably you meant `query { users { name } }`. The former still works because `users` is just treated as the name of the operation. – Daniel Rearden Jun 16 '19 at 23:57
  • I tried your suggestion `query { users { name } }`, unfortunately I am getting the same error... – TheSoul Jun 17 '19 at 21:09
  • @TheSoul Are you getting the *same* error as before? `Cannot query field "name" on type "Query"` – Daniel Rearden Jun 17 '19 at 22:22
  • Either way, this should probably be a separate question with all relevant code. The error you are seeing is unrelated to your original issue. – Daniel Rearden Jun 17 '19 at 22:24