I use nodejs with typescript for my project and I decided to use graphql but I have a problem I tried many things but I am still in one place when I tried to debug the problem I found that this error occurs when I use "BuildSchema" from "graphql" package.
so I give you detail and I hope you will help me
error description:
return new _GraphQLError.GraphQLError("Syntax Error: ".concat(description), undefined, source, [position]);
^
GraphQLError [Object]: Syntax Error: Expected Name, found "}".
my server.ts code:
import express from "express";
import bodyParser from "body-parser";
import cors from "cors";
import { connect } from "mongoose";
import { graphqlHTTP } from "express-graphql";
import { GraphQLSchema, buildSchema } from "graphql";
const app = express();
// ======= Env Configuration
import { config } from "dotenv";
config();
// ====== app middleware
app.use(bodyParser.json());
app.use(cors());
// ============= Graphql ============== \
import resolver from "./graphql/resolver";
import schema from "./graphql/schema";
const ss = buildSchema(schema);
app.use(
"/graphql",
graphqlHTTP({
graphiql: true,
schema: ss,
rootValue: resolver,
})
);
// ============ Connect to Server / Mongodb
const PORT = 5000;
connect(`${process.env.MONGO_URL}`)
.then(() => {
console.log(`mongoose connected...`);
app.listen(PORT, () => {
console.log(`server connected on ${PORT} port...`);
});
})
.catch((error) => {
throw error;
});
here is main schema file:
import { buildSchema, GraphQLSchema } from "graphql";
// ========== Schemas
import { UserSchema, UserRootQuery, UserRootMutation } from "./user.schema";
const Schema = `
${UserSchema}
type RootQuery {
${UserRootQuery}
}
type RootMutation {
${UserRootMutation}
}
schema {
query: RootQuery
mutation: RootMutation
}
`;
export default Schema;
here is one of the schema file:
export const UserSchema = `
type User {
userName: String!
age: Init!
country: String!
gender: String!
}
`;
export const UserRootQuery = `
users(userId: String): User
`;
export const UserRootMutation = `
`;
2
You have a typo somewhere in your schema definition string. Try removing the mutation entirely. Also
age: Init
is a typo26 mins ago
@AndyRay yes when I remove the mutation, the error is gone, but how I can use the mutation
11 mins ago