const TOURNAMENT_STEP_SHAPE_QUERY = graphql(` query tournamentStepGeneratedShape($stepId: ID!) { tournamentStepGeneratedShape(stepId: $stepId) { id name rounds { id name order games { id order matches { id order status } } } } }`);
const TOURNAMENT_TEAM_QUERY = graphql(` query tournamentTeam($tournamentId: ID!, $page: PageInfo!) { tournamentTeams(tournamentId: $tournamentId, page: $page) { nodes { id name members { playerProfileId status } } } }`);// Extract just the team typetype Team = ResultOf<typeof TOURNAMENT_TEAM_QUERY>['tournamentTeams']['nodes'][0];// Extract just the member typetype Member = ResultOf<typeof TOURNAMENT_TEAM_QUERY>['tournamentTeams']['nodes'][0]['members'][0];
Always give your queries descriptive names for better debugging:
const QUERY = graphql(` query getTournamentById($id: ID!) { tournament(id: $id) { id name } }`);
Extract common fragments
Use GraphQL fragments to reuse field selections:
const PlayerFragment = graphql(` fragment PlayerFields on Player { id username customFields { property value } }`);const QUERY = graphql(` query getPlayers($ids: [ID!]!) { players(ids: $ids, page: { first: 100 }) { nodes { ...PlayerFields } } }`, [PlayerFragment]);
Type your query results
Extract types for reusability across your application:
const QUERY = graphql(`...`);export type TournamentData = ResultOf<typeof QUERY>;export type TournamentVariables = VariablesOf<typeof QUERY>;
The graphql tagged template requires the GraphQL schema introspection to be up to date. Run yarn gql-tada:generate if you get type errors after schema updates.