0
votes

I have a small GraphQL query that I want to mock with Apollo's MockedProvider.

export const queryType = new GraphQLObjectType({
  name: 'Query',
  fields: {
    getUsername: {
      type: GraphQLString,
      resolve: ({ session }) => {
        if (session.isLoggedIn) return session.username;
        return null;
      }
    }
  }
});

export default new GraphQLSchema({ query: queryType, mutation: mutationType });

I tried adding it to the mocks array, but I couldn't figure out how to convert that query to a format it accepts. My code looks something like this:

const { getUsername } = queryType.getFields();
const mocks = [
  {
    request: { query: getUsername },
    result: { data: { getUsername: 'username' } }
  }
];

const app = (
  <MockedProvider mocks={mocks}>
    <App />
  </MockedProvider>
)

I tried to wrap it in gql tag, but it doesn't accept that either. How can I mock this query in my tests?

1

1 Answers

0
votes

You are doing it wrong. You have to first create a response for your query like this.

const addLanguageMock = {
  request: {
    query: Loaction_Query,
    variables: {
      name: 'हिंदी',
      iso_code: 'hin',
      name_in_english: 'hindi'
    }
  },
  result: {
    data: {}
  }
};

Then you can pass it like this.

  const mocks = [addLanguageMock]
  <MockedProvider mocks={mocks}>
    <App />
  </MockedProvider>