1stdibs / relay-mock-network-layer

Relay modern network layer that returns schema correct mock data
MIT License
36 stars 9 forks source link

Handle GraphQL errors #2

Closed hisapy closed 6 years ago

hisapy commented 6 years ago

Hello,

First of all, thanks for this simple but very useful lib. I started using it but I noticed that it wasn't showing me about some GraphQL errors I had, for example when not implementing mocks for custom scalars.

So I copied and pasted your implementation in my own project and changed it to the following:

require("babel-polyfill");

const {
  makeExecutableSchema,
  addMockFunctionsToSchema
} = require("graphql-tools");
const {
  graphql,
  printSchema,
  buildClientSchema,
  GraphQLScalarType
} = require("graphql");

function getNetworkLayer({ schema, mocks, resolvers }) {
  return async function fetchQuery(operation, variableValues) {
    if (typeof schema === "object" && schema.data) {
      schema = printSchema(buildClientSchema(schema.data));
    }

    const executableSchema = makeExecutableSchema({
      typeDefs: schema,
      resolvers
    });

    // Add mocks, modifies schema in place
    addMockFunctionsToSchema({
      schema: executableSchema,
      mocks,
      preserveResolvers: false
    });

    const result = await graphql(
      executableSchema,
      operation.text,
      null,
      null,
      variableValues
    );

    if (result.errors && result.errors.length > 0) {
      return Promise.reject(result);
    }

    return Promise.resolve(result);
  };
}

With that, you can get those mentioned errors in the QueryRenderer. As you can see, it uses async/await. I wanted send you a pull request but I didn't know how to handle the babel-polyfill dependency. Do you think we can include it as a peerDependency or we have to avoid using async/await?