/ snippets / typescript / graphql.json
graphql.json
 1  {
 2    "GraphQLModule.forRoot (Apollo)": {
 3      "prefix": ["ns_graphql_module_for_root", "GraphQLModule.forRoot"],
 4      "scope": "typescript",
 5      "body": ["GraphQLModule.forRoot<ApolloDriverConfig>({", "  driver: ApolloDriver,", "  typePaths: ['./**/*.graphql'],", "}),"],
 6      "description": "Configures GraphQLModule to use ApolloDriver with schema-first approach."
 7    },
 8    "upperDirectiveTransformer": {
 9      "prefix": ["ns_graphql_upper_directive_transformer", "upperDirectiveTransformer"],
10      "scope": "typescript",
11      "body": ["import { getDirective, MapperKind, mapSchema } from '@graphql-tools/utils';", "import { defaultFieldResolver, GraphQLSchema } from 'graphql';", "", "export function upperDirectiveTransformer(", "  schema: GraphQLSchema,", "  directiveName: string,", ") {", "  return mapSchema(schema, {", "    [MapperKind.OBJECT_FIELD]: (fieldConfig) => {", "      const upperDirective = getDirective(", "        schema,", "        fieldConfig,", "        directiveName,", "      )?.[0];", "", "      if (upperDirective) {", "        const { resolve = defaultFieldResolver } = fieldConfig;", "", "        // Replace the original resolver with a function that *first* calls", "        // the original resolver, then converts its result to upper case", "        fieldConfig.resolve = async function (source, args, context, info) {", "          const result = await resolve(source, args, context, info);", "          if (typeof result === 'string') {", "            return result.toUpperCase();", "          }", "          return result;", "        };", "        return fieldConfig;", "      }", "    },", "  });", "}"],
12      "description": "Utility to transform schema fields with a custom @upper directive example."
13    },
14    "transformSchema": {
15      "prefix": ["ns_graphql_transform_schema", "transformSchema"],
16      "scope": "typescript",
17      "body": "transformSchema: schema => upperDirectiveTransformer(schema, 'upper'),",
18      "description": "Applies the 'upper' directive transformation to the generated schema."
19    },
20    "buildSchemaOptions": {
21      "prefix": ["ns_graphql_build_schema_options", "buildSchemaOptions"],
22      "scope": "typescript",
23      "body": ["buildSchemaOptions: {", "  directives: [", "    new GraphQLDirective({", "      name: 'upper',", "      locations: [DirectiveLocation.FIELD_DEFINITION],", "    }),", "  ],", "},"],
24      "description": "Adds a custom @upper directive to schema-first GraphQL configuration."
25    },
26    "query": {
27      "prefix": ["ns_graphql_query", "query"],
28      "scope": "graphql",
29      "body": ["query {", "  $1", "}"],
30      "description": "GraphQL query operation template."
31    },
32    "mutation": {
33      "prefix": ["ns_graphql_mutation", "mutation"],
34      "scope": "graphql",
35      "body": ["mutation {", "  $1", "}"],
36      "description": "GraphQL mutation operation template."
37    },
38    "subscription": {
39      "prefix": ["ns_graphql_subscription", "subscription"],
40      "scope": "graphql",
41      "body": ["subscription {", "  $1", "}"],
42      "description": "GraphQL subscription operation template."
43    },
44    "fragment": {
45      "prefix": ["ns_graphql_fragment", "fragment"],
46      "scope": "graphql",
47      "body": ["fragment $1 on $2 {", "  $3", "}"],
48      "description": "GraphQL fragment template."
49    },
50    "type": {
51      "prefix": ["ns_graphql_type", "type"],
52      "scope": "graphql",
53      "body": ["type $1 {", "  $2", "}"],
54      "description": "GraphQL object type definition template."
55    },
56    "interface": {
57      "prefix": ["ns_graphql_interface", "interface"],
58      "scope": "graphql",
59      "body": ["interface $1 {", "  $2", "}"],
60      "description": "GraphQL interface type definition template."
61    }
62  }