I tried to use Apollo Federation and I create an user service and gateway. User service is working alone fine but Gateway is giving an error:
Error: A valid schema couldn't be composed. The following composition errors were found:
Syntax Error: Unexpected character: U+0130.
at IntrospectAndCompose.createSupergraphFromSubgraphList
Here is my code;
GATEWAY
import { ApolloGateway, IntrospectAndCompose } from "@apollo/gateway";
import { ApolloServer } from "@apollo/server";
import { expressMiddleware } from "@apollo/server/express4";
import { ApolloServerPluginDrainHttpServer } from "@apollo/server/plugin/drainHttpServer";
import express from "express";
import http from "http";
import { json } from "body-parser";
const startServer = async () => {
const gateway = new ApolloGateway({
supergraphSdl: new IntrospectAndCompose({
subgraphs: [
{ name: "userService", url: "http://localhost:4001" }
],
}),
});
const app = express();
const httpServer = http.createServer(app);
const server = new ApolloServer({
gateway,
plugins: [ApolloServerPluginDrainHttpServer({ httpServer })],
});
await server.start();
await new Promise<void>((resolve) =>
httpServer.listen({ port: 4000 }, resolve)
);
console.log(` Server ready at http://localhost:4000/graphql`);
};
startServer();
USER SERVICE
index.ts
import { ApolloServer } from "@apollo/server";
import { buildSubgraphSchema } from "@apollo/subgraph";
import gql from "graphql-tag";
import { resolvers } from "./graphql/resolver";
import { startStandaloneServer } from "@apollo/server/standalone";
import { readFileSync } from "fs";
const mongoose = require("mongoose");
const typeDefs = gql(
readFileSync("src/graphql/types/user.graphql", { encoding: "utf-8" })
);
import { DB_URI } from "../database-config";
require("dotenv").config();
const createDatabaseConnection = async () => {
try {
await mongoose.connect(DB_URI);
console.log("User Service: Connected to DB");
} catch (error) {
console.log(error);
}
};
const startUserServiceServer = async () => {
const server = new ApolloServer({
schema: buildSubgraphSchema({ typeDefs, resolvers }),
});
await createDatabaseConnection();
const { url } = await startStandaloneServer(server, {
listen: { port: 4001 },
});
console.log(`User Service: Server ready at ${url}`);
};
startUserServiceServer();
user.graphql
extend schema
@link(url: "https://specs.apollo.dev/federation/v2.0", import: ["@key"])
type Query {
me: User
}
type Mutation {
addUser(fields: UserInput): User
updateUser(fields: UserInput): User
}
type User @key(fields: "id") {
id: ID
fullName: String
phone: String
avatarURL: String
password: String
email: String
address: String
createdAt: String
updatedAt: String
}
input UserInput {
id: ID
fullName: String
phone: String
avatarURL: String
password: String
email: String
address: String
}
I tried to change encoding style of user.graphl
as utf16le
but it didn't work. Also I use Turkish as a default language on my computer. When I switched to English the problem was gone but I couldn't understand what the problem is.
The
Name
in GraphQL is defined to be limited to the regular expression/[_A-Za-z][_0-9A-Za-z]*/
.The Turkish captial I (
U+0130
orİ
) does not match this regular expression and is considered invalid.[source: GraphQL Spec]