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

How deal with complex queries? #52

Open
villander opened this issue Feb 21, 2022 · 3 comments
Open

How deal with complex queries? #52

villander opened this issue Feb 21, 2022 · 3 comments

Comments

@villander
Copy link

I'm getting a bunch of errors when I try to use queries with variables.

image

// app/mirage/server.js

import { createServer } from "miragejs"
import { createGraphQLHandler } from "@miragejs/graphql"
import graphQLSchema from "app/gql/schema.gql"

export function makeServer() {
  return createServer({
    routes() {
      const graphQLHandler = createGraphQLHandler(graphQLSchema, this.schema);

      this.post("/graphql", graphQLHandler)
    }
  })
}

// schema

import { gql } from 'glimmer-apollo'

export const graphQLSchema = gql`
  type CurrentAccount {
    id: ID!
    name: String!
    flags: [String]!
    memberships: [Membership]
  }

  type Account {
    id: ID!
    accountType: String!
    isActive: Boolean!
    name: String!
  }

  type CurrentUser {
    id: ID!
    name: String!
    email: String!
    imageUrl: String!
    phone: String!
    isPeekAdmin: Boolean!
    currentAccountRoles: [String]
    accounts: [Account]
  }

  type Membership {
    id: ID!
    name: String!
    membershipVariants: [MembershipVariant!]!
  }

  type MembershipVariant {
    colorHex: String!
    creditsPerTerm: Int!
    description: String!
    externalName: String!
    id: ID!
    imageUrl: String!
    internalName: String!
  }

  type Query {
    currentAccount: CurrentAccount!
    currentUser: CurrentUser!
    memberships(id: ID!): [Membership!]!
  }
 `

// query

query CurrentAccountMembershipVariant($membershipId: ID, $variantId: ID) {
  currentAccount {
    id
    memberships(id: $membershipId) {
      id
      membershipVariants(id: $variantId) {
        id
        internalName
        externalName
        description
        currency
        creditsPerTerm
        imageUrl
        colorHex
        validBetween
        validFor {
          amount
          unit
        }
        creditWindow {
          unit
          amount
        }
      }
    }
  }
}

Could you please give some help here @jneurock I might be missing something?

@jneurock
Copy link
Collaborator

I think you're running into the auto-filtering logic of Mirage GraphQL (see #34).

In this case, you'll want to add a custom resolver.

Something like this:

// app/mirage/server.js

import { createServer } from "miragejs"
import { createGraphQLHandler } from "@miragejs/graphql"
import graphQLSchema from "app/gql/schema.gql"

export function makeServer() {
  return createServer({
    routes() {
      const graphQLHandler = createGraphQLHandler(graphQLSchema, this.schema, {
        resolvers: {
          Query: {
            currentAccount(_obj, args, context) {
              let currentAccount; // Fetch current account from the Mirage DB or build it up however you need
              let membership = context.mirageSchema.memberships.find(args.membershipId);
              let membershipVariant = context.mirageSchema.membershipVariants.find(args.variantId);

              return {
                ...currentAccount,
                memberships: [{
                   ...membership,
                   membershipVariants: [{
                     ...membershipVariant
                   }],
                }],
              };
            }
          }
        }
      });

      this.post("/graphql", graphQLHandler)
    }
  })
}

Apologies for any typos or incorrect method invocations from Mirage's API. IDK how you want to fetch or create the currentAccount record so I just left a comment there in the example.

LMK if this helps for now.

@sebamza17

This comment was marked as off-topic.

@jneurock
Copy link
Collaborator

Hey @sebamza17, since this new issue isn't directly related to the original, shall we discuss elsewhere? The Mirage Discord works well for me or you can open another GH issue.

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

3 participants