Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding context object to handler to mock auth #60

Open
trevordammon opened this issue Jul 16, 2021 · 0 comments
Open

Adding context object to handler to mock auth #60

trevordammon opened this issue Jul 16, 2021 · 0 comments

Comments

@trevordammon
Copy link

The mirage GraphQL docs mention that I am able to add a context object to my handler to mock auth, however I cannot find any examples of this being done, nor have I been able to successfully implement it on my own.

I want to add a context object I found in the Apollo Documentation

  context({ req }) {
    const token = req.headers.authorization;
    const user = getUserFromToken(token);
    return { ...db, user, createToken };
  },

My graphQLHandler looks like this:

export function makeServer(environment) {
  let server = createServer({
    models: {
      Person: Model.extend({}),
    },
    routes() {
      const graphQLHandler = createGraphQLHandler(graphqlSchema, this.schema, {
        resolvers: {
          Query: {
            people(obj, args, context, info) {
              const people = mirageGraphQLFieldResolver(
                obj,
                args,
                context,
                info,
              );

              const peopleArray = server.schema.db._collections.filter(
                (record) => record.name === 'people',
              );

              if (args.id_) {
                console.log(' not empty');
                return peopleArray[0]._records.filter(
                  (person) => person.person_id.id_ === args.id_,
                );
              }
              return peopleArray[0]._records;
            },
          },
          Person: {
            name(person) {

              return person.name;
            },
            person_id(person) {

              return person.person_id;
            },
          },
          Mutation: {
            create_person(obj, args, context, info) {
              const personPayload = server.schema.create('Person', {
                name: {
                  first_name: args.first_name,
                  last_name: args.last_name,
                  middle_name: args.middle_name,
                },
                person_id: { id_: Math.random().toString() },
              });

              return {
                status: '201',
                error: 'special error',
                person: {
                  ...personPayload.attrs,
                },
              };
            },
            update_person(obj, args, context, info) {

              const people = server.schema.db._collections.filter(
                (record) => record.name === 'people',
              );

              const updatedPerson = people[0].update(args.id_, {
                name: {
                  first_name: args.first_name,
                  last_name: args.last_name,
                  middle_name: args.middle_name,
                },
                person_id: { id_: args.id_ },
              });

              return {
                status: '201',
                error: 'special error',
                person: {
                  ...updatedPerson,
                },
              };
            },
            delete_person(obj, args, context, info) {

              const people = server.schema.db._collections.filter(
                (record) => record.name === 'people',
              );

              people[0].remove(args.id_);

            },
          },
          PersonResponse: {
            person(response) {
              return response.person;
            },
          },
        },
      });

      this.post('/graphql', graphQLHandler);
    },
    seeds(server) {
      server.schema.create('Person', {
        name: { first_name: 'Joe', last_name: 'Smith' },
        person_id: { id_: '1' },
        id: '1',
      });
      server.schema.create('Person', {
        name: { first_name: 'Jon', last_name: 'Schmidt' },
        person_id: { id_: 'yyy' },
        id: 'yyy',
      });
    },
  });
  return server;
}

If anyone knows how to implement the context object I would greatly appreciate some guidance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant