What is the design pattern for doing Apollo serverside logic

What is the design pattern for doing Apollo serverside logic


0

To learn GraphQL and Apollo I am working on a Tic Tac Toe game. Currently the way it works is that the server stores all game information so when the client clicks a square, it sends a mutation to the server which updates the GameState type and returns the new state. What I would like is for the server to handle all game logic like checking if a move is legal, playing the computers move, checking for winners, and then returning the new GameState.

My initial thought is to just put all that logic in the mutation resolver but in the tutorial I followed it strongly recommended keeping resolvers as small as possible so that does not seem like the best design. I cannot think of another way to do it though, are Apollo servers generally not used this way?

Potentially relevant code:

Schema:

const typeDefs = gql`
    type GameState {
        board: [[String!]!]!
        status: String!
        winner: String
    }
    type Mutation {
        placeO(x: Int!, y: Int!): GameState
    }
    type Query {
        "Query to get tracks array for the homepage grid"
        getGameState: GameState
    }
`;

Mutation Resolver:

  Mutation: {
    placeO: (_, { x, y }, { dataSources }) => {
      // check_legal_move(x, y, dataSources.state.board); ??
      dataSources.state.board[x][y] = 'O';
      // computer_move(dataSources.state.board); ??
      return dataSources.state;
    }
  },

Client Mutation Request:

const PLACE_O = gql`
    mutation placeO($x: Int!, $y: Int!) {
        placeO(x: $x, y: $y) {
            board,
            status,
            winner,
        }
    }
`;

Share


Load 3 more related questions


Show fewer related questions

0

Reset to default



Leave a Reply

Your email address will not be published. Required fields are marked *