Search code examples
apolloapollo-federation

Rover is composing supergraph with unexpected character


I'm following the docs trying to get an apollo gateway up and running. I'm using rover to compose my subgraph with this command: rover supergraph compose --config ./supergraph-config.yaml > supergraph.graphql

The command works, however when I try to start up my gateway, I get this error: GraphQLError: Syntax Error: Unexpected character: U+FFFD. It seems the supergraph compose command is adding some invalid chatacters for some reason. When I print out the file as a string I see these 2 characters at the top of the file: ��.

Example:

��
schema
  @link(url: "https://specs.apollo.dev/link/v1.0")
  @link(url: "https://specs.apollo.dev/join/v0.2", for: EXECUTION)
{
  query: Query
}
...

I also haven't been able to find any issues on stack overflow or github so I'm not really sure what the issue is.

Gateway:

const { ApolloServer, gql } = require('apollo-server');
const { ApolloGateway } = require('@apollo/gateway');
const { readFileSync } = require('fs');
const path = require("path");

const schemaString = readFileSync("../supergraph.graphql").toString()
const supergraphSdl = gql` ${schemaString} `;

// Initialize an ApolloGateway instance and pass it
// the supergraph schema
const gateway = new ApolloGateway({
  supergraphSdl,
});

// Pass the ApolloGateway to the ApolloServer constructor
const server = new ApolloServer({
  gateway,
});

server.listen().then(() => {
  console.log(`🚀 Gateway ready `);
});

Subgraph:

const { ApolloServer, gql } = require('apollo-server');
const { buildSubgraphSchema } = require('@apollo/subgraph');

interface User {
    id: string
    username: string
}

const typeDefs = gql`
  extend schema
    @link(url: "https://specs.apollo.dev/federation/v2.0",
          import: ["@key", "@shareable"])

  type Query {
    me: User
  }

  type User @key(fields: "id") {
    id: ID!
    username: String
  }
`;

const resolvers = {
  Query: {
    me() {
      return { id: "1", username: "@ava" }
    }
  },
  User: {
    __resolveReference(user: User, { fetchUserById }: any){
      return fetchUserById(user.id)
    }
  }
}

const server = new ApolloServer({
  schema: buildSubgraphSchema({ typeDefs, resolvers })
});

server.listen(3000).then(({ url }) => {
    console.log(`🚀 Server ready at ${url}`);
});

Solution

  • The file that rover was generating was encoded in utf16le, the problem was that I was reading the file in utf8.

    Reading the file in 'utf16le' encoding format worked.

    const schemaString = await readFileSync('../supergraph.graphql', 'utf16le');