Apollo-server: How do you stop/change the "Hello World" response for fields not found in the response?

Created on 21 Jan 2019  路  5Comments  路  Source: apollographql/apollo-server

Sorry if this comes across as a naive question, but I've always been bothered by the fact that Apollo Server will return "Hello World" for fields which are not found (i.e. empty or null values). I would like to change this behavior - either by replacing the text or stopping this behavior all together.

I haven't found any documentation on this behavior and I'm curious why Apollo server returns "Hello World" if the field doesn't have a value. Is this part of the spec?

Thanks,
Henry

Most helpful comment

Okay I found what was causing this - I started with some boilerplate Apollo server code and tucked away in a module was the following:

addMockFunctionsToSchema({
  mocks: {},
  preserveResolvers: true,
  schema
});

Deleting this little bit of code stopped all the Hello World mocks :-)

All 5 comments

You most likely have mocks option set to true when creating the server.

https://www.apollographql.com/docs/apollo-server/features/mocking.html#Default-mock-example

My entire index.ts file

import * as express from "express";
import { ApolloServer } from "apollo-server-express";
import { schema } from "./schema";
import { context } from "./data";
import logger from "./util/logger";

const app = express();
const server = new ApolloServer({
  schema,
  context,
  introspection: true,
  formatError: error => {
    logger.error(error);
    // return error;
    return {
      message: error.message,
      status: error.extensions.code
        ? error.extensions.code.toUpperCase()
        : "INTERNAL_GRAPHQL_ERROR"
    };
  }
});

const port = process.env.PORT || 4000;

server.applyMiddleware({ app }); // app is from an existing express app

app.listen({ port }, () =>
  logger.info(
    `馃殌 Server ready at http://localhost:${port}${server.graphqlPath}`
  )
);

Fairly easy to recreate. In any graphql schema you have, add a field to your return type:

 type PersonDetails {
     firstName: String!
     lastName: String!
     blah: String
  }

Don't return anything for the "blah" value but ask for it in your query.

Your result:
{
      "firstName": "Hank",
      "lastName": "Kramer",
      "blah": "Hello World"
}

So on a hunch I looked at the underlying graphql-tools repo and found references to "Hello World" in the code. I believe this is returning "Hello World" for fields which are not found/null.

I'm guessing some parameters I'm passing or NOT passing into Apollo Server is creating this behavior...

Okay I found what was causing this - I started with some boilerplate Apollo server code and tucked away in a module was the following:

addMockFunctionsToSchema({
  mocks: {},
  preserveResolvers: true,
  schema
});

Deleting this little bit of code stopped all the Hello World mocks :-)

Was this page helpful?
0 / 5 - 0 ratings