graphql-import icon indicating copy to clipboard operation
graphql-import copied to clipboard

Error: Query root type must be provided #610

Open lpender opened this issue 7 years ago • 6 comments

I'm receiving the error:

Error: Query root type must be provided

Here is my schema:

schema.graphql

schema {
  query: ClientQuery
   ...
}

type ClientQuery {
  ...
}

I am importing it with graphql-importer

import { importSchema } from 'graphql-import'

const baseTypeDefs = importSchema('../pg-fresh-client/schema/schema.graphql')

Shouldn't this be valid?

lpender avatar Feb 06 '18 13:02 lpender

@lpender - did you resolve this issue?

relaxedtomato avatar Feb 19 '18 16:02 relaxedtomato

here's the issue — graphql-import gathers definition types, but unintentionally skips over some important types like the SchemaType, leading to the above issue.

in particular, in https://github.com/graphcool/graphql-import/blob/e5c4b078e4747aa6e3ffec37139451f85011478f/src/index.ts#L263-L271 we should actually be allowing the SchemaTypeDefinition. im not sure what the behavior should be when you find multiple SchemaTypeDefinitions, though, should they be combined? should we throw a warning or just fail if there are collisions, given that we shouldn't expect child documents to re-define the schema type?

jnwng avatar Apr 06 '18 16:04 jnwng

i believe

https://github.com/graphql/graphql-js/blob/92cb4a039fd0fd8181c4fc16a7caefb8f41fcbe5/src/utilities/buildASTSchema.js#L225-L241

is where graphql-js parses out the proper types, and here is where it defaults back to Query if this SchemaTypeDefinition isn't set:

https://github.com/graphql/graphql-js/blob/92cb4a039fd0fd8181c4fc16a7caefb8f41fcbe5/src/utilities/buildASTSchema.js#L171-L177

jnwng avatar Apr 06 '18 16:04 jnwng

Any fix for this?

RyannGalea avatar May 15 '18 14:05 RyannGalea

Remember typedefs its just a template string, so.

import path from 'path';
import { importSchema } from 'graphql-import';
import { makeExecutableSchema } from 'graphql-tools';
import resolvers from './resolvers';

const typeDefs = importSchema(path.join(__dirname, './root/Query.graphql'));

export default makeExecutableSchema({
  typeDefs: `
    ${typeDefs}
    
    schema {
      query: Query,
    }
  `,
  resolvers,
});

Works

Katyadelarosa avatar May 17 '18 23:05 Katyadelarosa

Just provide type Query with any method.

const { ApolloServer, gql } = require("apollo-server");

const todos = [
  { task: "work for today", completed: true },
  { task: "finish home work", completed: false }
];
const typeDefs = gql`
  type Todo {
    task: String
    completed: Boolean
  }

  type Query {
    getTodos: Todo
  }
`;
const server = new ApolloServer({
  typeDefs: typeDefs
});

server.listen().then(({ url }) => {
  console.log(`server listening on ${url}`);
});

ali-turki avatar May 04 '19 18:05 ali-turki