RE: RE: graphQL advanced: 고오급 graphQL 테크닉 - Mutation 후 Query를 한반에!
You are viewing a single comment's thread from:

RE: graphQL advanced: 고오급 graphQL 테크닉 - Mutation 후 Query를 한반에!

acidsound(48)
Published in
#kr
Words
8
Reading
1 min
Listen
Play
8y

launchpad가 망할 것을 대비하여 전체 소스를 댓글로 첨부한다.

import { makeExecutableSchema } from 'graphql-tools';

const ballMaxCount = 5;
let ballCount = ballMaxCount;
let scores = [];

const typeDefs = `
  type Score {
    result: Boolean
  }
  type Query {
    getBallCount: Int
    getScores: [ Score ]
  }
  type Mutation {
    submitKick: Query
  }
`;

const resolvers = {
  Query: {
    getBallCount: (root, args, context) => ballCount,
    getScores: ()=> scores
  },
  Mutation: {
    submitKick: (root, args, context) => {
      if (ballCount === 0) {
        ballCount = ballMaxCount;
        scores = [];
      }
      let result = !~~(Math.random()+0.5);
      scores.push( { result } );
      ballCount--;
      return {
        result
      };
    }
  }
};

export const schema = makeExecutableSchema({
  typeDefs,
  resolvers,
});

export function context(headers, secrets) {
  return {
    headers,
    secrets,
  };
};
@acidsound: launchpad가 망할 | Ecency