I've been getting this error for a while now when I am trying to compile my code.
Error: Unknown type "Query".
index.js
import { ApolloServer } from 'apollo-server';
import path from 'path';
import { fileLoader, mergeTypes, mergeResolvers } from 'merge-graphql-schemas';
import models from './models/index';
const typeDefs = mergeTypes(fileLoader(path.join(__dirname, './schema')));
const resolvers = mergeResolvers(
fileLoader(path.join(__dirname, './resolvers'))
);
const server = new ApolloServer({ typeDefs, resolvers, context: models });
models.sequelize.sync({ force: true }).then(x => {
server.listen().then(({ url }) => {
console.log(`馃殌 Server ready at ${url}`);
});
});
schema/user.js
export default {
Query: {
getUser: (parent, { id }, { models }) => {
models.User.findOne({ where: { id } });
},
allUsers: (parent, args, { models }) => {
models.User.findAll();
}
},
Mutation: {
createUser: (parent, args, { models }) => {
models.User.create(args);
}
}
};
Can somebody please help me because the error message doesn't tell me much.
I am new to graphql aswell and I had the same problem. I am using merge-graphql-schemas aswell. My problem was that I was using Typescript and my .graphql files weren't copied over to the output directory (by default directory named dist, check out your tsconfig.json) by tsc command. Now I am using cpx (a node package) to copy over all my .graphql files to my tsc output directory.
@Istjustaname can you provide a Github repo to share. So we can see how you did it? I have the same problem as @reeversedev
I'm using cpx already to move around items that TS does not handle over to my output directory. Including *.graphql. I get the same results so far. I'm just starting a project fresh and I don't have the Query object specified anywhere in my source code. @Istjustaname
I'm digging through graphql/validations/validation.js where the error is output.
i just logged out:
console.log('dast',documentAST.definitions[0].operationTypes);
var errors = validateSDL(documentAST);
console.log(errors[0].locations);
[ { kind: 'OperationTypeDefinition',
operation: 'query',
type: { kind: 'NamedType', name: [Object], loc: [Object] },
loc: { start: 11, end: 23 } },
{ kind: 'OperationTypeDefinition',
operation: 'mutation',
type: { kind: 'NamedType', name: [Object], loc: [Object] },
loc: { start: 26, end: 44 } } ]
notice it is passing around this object with operation query. I don't know how to control or understand this behavior but I hope this helps.
see here
https://github.com/okgrow/merge-graphql-schemas/issues/155
@RichardLitt it needs for https://github.com/prisma/prisma-binding
For base schema not define Query and other. Then prisma generate complete schema. When merged schema contains Query/Mutation/Subscription prisma create tables in database for them.
It is expected behavior. Is this item closed now?
@reeversedev
For me this is still open because honestly, I've not been able to reproduce more.
Also experiecing this issue.
Error: Unknown type "Query".
at assertValidSDL (/home/user/Code/App/server/node_modules/graphql/validation/validate.js:80:11)
I am experiencing the same issue. Here's my repo:
https://github.com/nareshbhatia/graphql-bookstore/tree/master/apollo-bookstore-server
For build instructions, please see README.md:
Unknown type "Query" error.Fixed. It was an easy one! I was not copying the typedef files to the dist folder. This was required for merge-graphql-schemas to stitch the schema together. You can now see the complete working solution here.
Thanks for reporting this issue originally!
I'm going to close this issue since it hasn't received a lot of traction and could have been resolved already, plus the follow-up that @nareshbhatia provided seems to be working properly.
If this is still a problem, would someone who's experiencing the problem (or anyone who comes across this issue and is able to assist) mind building a reproduction of the problem in to a runnable CodeSandbox reproduction using the latest version of Apollo Server and sharing the link to that CodeSandbox in this issue?
I'm happy to re-open if this is still occurring and someone can provide a runnable reproduction of the original issue. Thanks again!
Hi, I have the same problem. This is the repo: https://github.com/alexahdp/harity_questions/tree/graphql-problem
just do
npm ci
npm run start:dev
to reproduce it.
Fixed. It was an easy one! I was not copying the typedef files to the dist folder. This was required for merge-graphql-schemas to stitch the schema together. You can now see the complete working solution here.
i can't get this to work. tried to run yarn and yarn build but with no success. still getting Error: Unknown type "Query".
@eladnm, are you running my project? Can you confirm that yarn build is running "copy-to-dist": "copyfiles --up 1 src/graphql/typedefs/* dist" and the typedef files are actually being copied to the dist folder?
@eladnm, are you running my project? Can you confirm that yarn build is running
"copy-to-dist": "copyfiles --up 1 src/graphql/typedefs/* dist"and the typedef files are actually being copied to the dist folder?
Hi, I am actually running a different project but this is the only place I could find a reference for this type of error.. can you please explain about this error so I can try to resolve it by myself? Thanks!!
@eladnm, in my use case I am creating "mini" GraphQL schemas and merging them to one final schema using a library called merge-graphql-schemas. This works in development mode because merge-graphql-schemas can find the "mini" schemas in the src folder. However when I build for production mode (i.e. compile TypeScript into JavaScript), then merge-graphql-schemas cannot find the scemas in the dist folder. Hence the error. To prevent this error, I have to copy the mini schemas to the dist folder. Hope that explains.
@reeversedev , I know it's late but i think your error seems to be your folder location ,in path.join(__dirname,relative_folder_location)
eg: if schema is in slack-clone-server/src/schema
then it should be as path.join(__dirname,./src/schema)
Most helpful comment
I am new to graphql aswell and I had the same problem. I am using merge-graphql-schemas aswell. My problem was that I was using Typescript and my .graphql files weren't copied over to the output directory (by default directory named dist, check out your tsconfig.json) by tsc command. Now I am using cpx (a node package) to copy over all my .graphql files to my tsc output directory.