Apollo-link: Mocked Link for Testing

Created on 7 Sep 2017  路  11Comments  路  Source: apollographql/apollo-link

This is more a question then issue, but could be part of new features.
I am trying to create a link that would return mocks.

Basically I'm trying to use apollo-test-utils functionality with Apollo Client 2.0.

would something like this do?

import { ApolloLink, Operation, FetchResult, Observable } from 'apollo-link-core';
import { ApolloFetch, createApolloFetch } from 'apollo-fetch';

import { print } from 'graphql/language/printer';
import { IExecutableSchemaDefinition } from 'graphql-tools/dist/Interfaces';
import { graphql, GraphQLSchema } from 'graphql';

export default class MockLink extends ApolloLink {
  schema: GraphQLSchema;
  rootValue: any;
  context: any;

  constructor(params?: { schema: GraphQLSchema; rootValue?: any; context?: any }) {
    super();
    this.schema = params.schema;
    this.rootValue = params.rootValue;
    this.context = params.context;
  }

  public request(operation: Operation): Observable<FetchResult> | null {
    const request = {
      ...operation,
      query: print(operation.query)
    };

    return new Observable<FetchResult>(observer => {
      graphql(this.schema, request.query, this.rootValue, this.context, request.variables, request.operationName)
        .then(data => {
          if (!observer.closed) {
            observer.next(data);
            observer.complete();
          }
        })
        .catch(error => {
          if (!observer.closed) {
            observer.error(error);
          }
        });
    });
  }
}

used like this

const typeDefs = `
Query {
...
}
`;

const mocks = {
    Query: () => ...,
    Mutation: () => ...
  };

  const schema = makeExecutableSchema({ typeDefs });
  addMockFunctionsToSchema({
    schema,
    mocks
  });

  const apolloCache = new InMemoryCache(window.__APOLLO_STATE_);

  const graphqlClient = new ApolloClient({
    cache: apolloCache,
    link: new MockLink({ schema }) as any
  });

Most helpful comment

Hi, I have tested it and it works rather well. Would you be interested in PR for this? I would call it "LocalLink" instead of "MockLink"

All 11 comments

Hi, I have tested it and it works rather well. Would you be interested in PR for this? I would call it "LocalLink" instead of "MockLink"

I think this would be great for server side rendering / querying directly against the schema/database.

Yeah, looks like a good use for it. So far no response from devs, I guess they are busy with 2.0 release and once it's done we will get attention. Not in hurry for this.

I think this is a great idea! Please PR it 馃帀 it would be so great for SSR too!

馃憤
This would be very handy as replacement for apollo-test-utils!
Any progress on this?

Many thanks

Any progress on this ? It becomes much needed !

also in need in here!

Worked for me

I just did PR in #227
Will wait for review now.

Great work @tomitrescak! Thank you so much!

FYI for those visiting later, module is published as https://www.npmjs.com/package/apollo-link-schema

Was this page helpful?
0 / 5 - 0 ratings

Related issues

bwhitty picture bwhitty  路  3Comments

valerybugakov picture valerybugakov  路  5Comments

NicholasLYang picture NicholasLYang  路  4Comments

ChenRoth picture ChenRoth  路  4Comments

SaschaDens picture SaschaDens  路  3Comments