Coder Social home page Coder Social logo

How deal with complex queries? about graphql HOT 3 OPEN

miragejs avatar miragejs commented on June 15, 2024
How deal with complex queries?

from graphql.

Comments (3)

jneurock avatar jneurock commented on June 15, 2024 1

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.

from graphql.

jneurock avatar jneurock commented on June 15, 2024

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.

from graphql.

sebamza17 avatar sebamza17 commented on June 15, 2024

@jneurock
Thank you for your answer!
I'm working with @villander on this, but for us, queries are working out of the box; but we're having an issue with mutations

we're adding this to our graphQLHandler as resolvers to handle a specific mutation we're trying to test

  UpdateMembershipInstanceResponse: {
    resolveType: () => {
      return 'UpdateMembershipInstanceSuccess'
    }
  }

  Mutation: {
    updateMembershipInstance: (obj, args, context) => {
      const { mirageServer } = context
      const { id: membershipInstanceId } = args.input
      const membershipInstance = mirageServer.schema.membershipInstances.find(membershipInstanceId)

      if (membershipInstance) {
        membershipInstance.update(args.input)
      }

      return mirageGraphQLFieldResolver(...arguments)
    }
  }

and in our schema

  // miragejs/schema.js
  ...
  type MembershipInstance {
    ...
  }

  type Mutation {
    updateMembershipInstance(input: UpdateMembershipInstanceInput): UpdateMembershipInstanceResponse
  }

  input UpdateMembershipInstanceInput {
    ...
  }

  union UpdateMembershipInstanceResponse = UpdateMembershipInstanceSuccess | Error

  type UpdateMembershipInstanceSuccess {
    membershipInstance: MembershipInstance
  }

  type Error {
    message: String
  }
  ...

this basically works, it grabs mirage data and updates what we need to update, but as per these docs https://github.com/miragejs/graphql#example-deleting-a-person, this should return mirageGraphQLFieldResolver(...arguments), but we're having an issue with (probably) the resolver for UpdateMembershipInstanceResponse, this is what we're getting

"Abstract type "UpdateMembershipInstanceResponse" must resolve to an Object type at runtime for field "Mutation.updateMembershipInstance". Either the "UpdateMembershipInstanceResponse" type should provide a "resolveType" function or each possible type should provide an "isTypeOf" function."

any ideas would be welcome!

from graphql.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.