graphql-static-binding
Advanced tools
Comparing version
@@ -5,2 +5,3 @@ "use strict"; | ||
Main: renderMainMethod, | ||
Header: function () { return ''; }, | ||
}; | ||
@@ -7,0 +8,0 @@ function renderMainMethod(queryType, mutationType, subscriptionType) { |
@@ -14,2 +14,3 @@ "use strict"; | ||
Main: renderMainMethod, | ||
Header: renderHeader, | ||
}; | ||
@@ -23,9 +24,12 @@ var scalarMapping = { | ||
}; | ||
function renderHeader(schema) { | ||
return "import { FragmentReplacements } from 'graphcool-binding/dist/src/extractFragmentReplacements';\nimport { GraphcoolLink } from 'graphcool-binding/dist/src/GraphcoolLink';\nimport { buildFragmentInfo, buildTypeLevelInfo } from 'graphcool-binding/dist/src/prepareInfo';\nimport { GraphQLResolveInfo, GraphQLSchema } from 'graphql';\nimport { GraphQLClient } from 'graphql-request';\nimport { SchemaCache } from 'graphql-schema-cache';\nimport { delegateToSchema } from 'graphql-tools';\nimport { sign } from 'jsonwebtoken';\n\n// -------------------\n// This should be in graphcool-binding\ninterface BindingOptions {\n fragmentReplacements?: FragmentReplacements\n endpoint: string\n secret: string\n}\n\ninterface BaseBindingOptions extends BindingOptions {\n typeDefs: string\n}\n\nconst schemaCache = new SchemaCache()\n\nclass BaseBinding {\n private remoteSchema: GraphQLSchema\n private fragmentReplacements: FragmentReplacements\n private graphqlClient: GraphQLClient\n\n constructor({\n typeDefs,\n endpoint,\n secret,\n fragmentReplacements} : BaseBindingOptions) {\n \n fragmentReplacements = fragmentReplacements || {}\n\n const token = sign({}, secret)\n const link = new GraphcoolLink(endpoint, token)\n\n this.remoteSchema = schemaCache.makeExecutableSchema({\n link,\n typeDefs,\n key: endpoint,\n })\n\n this.fragmentReplacements = fragmentReplacements\n\n this.graphqlClient = new GraphQLClient(endpoint, {\n headers: { Authorization: `Bearer ${token}` },\n })\n }\n\n delegate<T>(operation: 'query' | 'mutation', prop: string, args, info?: GraphQLResolveInfo | string): Promise<T> {\n if (!info) {\n info = buildTypeLevelInfo(prop, this.remoteSchema, operation)\n } else if (typeof info === 'string') {\n info = buildFragmentInfo(prop, this.remoteSchema, operation, info)\n }\n\n return delegateToSchema(\n this.remoteSchema,\n this.fragmentReplacements,\n operation,\n prop,\n args || {},\n {},\n info,\n )\n }\n\n async request<T = any>(\n query: string,\n variables?: { [key: string]: any },\n ): Promise<T> {\n return this.graphqlClient.request<T>(query, variables)\n }\n}\n// -------------------\n\nconst typeDefs = `\n" + schema + "`"; | ||
} | ||
function renderMainMethod(queryType, mutationType, subscriptionType) { | ||
return "export const binding: Schema = {\n query: {\n" + renderMainMethodFields(queryType.getFields()) + "\n }" + (mutationType ? ",\n mutation: {\n" + renderMainMethodFields(mutationType.getFields()) + "\n }" : '') + (subscriptionType ? ",\n subscription: {\n" + renderMainMethodFields(subscriptionType.getFields()) + "\n }" : '') + "\n}"; | ||
return "export class Binding extends BaseBinding {\n \n constructor({ endpoint, secret, fragmentReplacements} : BindingOptions) {\n super({ typeDefs, endpoint, secret, fragmentReplacements});\n }\n \n query: Query = {\n" + renderMainMethodFields('query', queryType.getFields()) + "\n }" + (mutationType ? "\n\n mutation: Mutation = {\n" + renderMainMethodFields('mutation', mutationType.getFields()) + "\n }" : '') + "\n}"; | ||
} | ||
function renderMainMethodFields(fields) { | ||
function renderMainMethodFields(operation, fields) { | ||
return Object.keys(fields).map(function (f) { | ||
var field = fields[f]; | ||
return " " + field.name + ": (args, info): " + renderFieldType(field.type) + (!graphql_1.isNonNullType(field.type) ? ' | null' : '') + " => { return /* TODO: Get actual implementation here from graphql-binding */ }"; | ||
return " " + field.name + ": (args, info): Promise<" + renderFieldType(field.type) + (!graphql_1.isNonNullType(field.type) ? ' | null' : '') + "> => super.delegate('" + operation + "', '" + field.name + "', args, info)"; | ||
}).join(',\n'); | ||
@@ -42,3 +46,3 @@ } | ||
var field = type.getFields()[f]; | ||
return " " + field.name + ": (args: {" + (field.args.length > 0 ? ' ' : '') + field.args.map(function (f) { return renderFieldName(f) + ": " + renderFieldType(f.type); }).join(', ') + (field.args.length > 0 ? ' ' : '') + "}, info: any) => " + renderFieldType(field.type) + (!graphql_1.isNonNullType(field.type) ? ' | null' : ''); | ||
return " " + field.name + ": (args: {" + (field.args.length > 0 ? ' ' : '') + field.args.map(function (f) { return renderFieldName(f) + ": " + renderFieldType(f.type); }).join(', ') + (field.args.length > 0 ? ' ' : '') + "}, info?: GraphQLResolveInfo | string) => Promise<" + renderFieldType(field.type) + (!graphql_1.isNonNullType(field.type) ? ' | null' : '') + ">"; | ||
}).join('\n'); | ||
@@ -45,0 +49,0 @@ return renderInterfaceWrapper(type.name, type.description, type.getInterfaces(), fieldDefinition); |
@@ -16,7 +16,9 @@ "use strict"; | ||
.sort(function (a, b) { return ast.getType(a).constructor.name < ast.getType(b).constructor.name ? -1 : 1; }); | ||
// Special case 4: header | ||
var generatedClass = [generator.Header(schema)]; | ||
// Process all types | ||
var generatedClass = typeNames.map(function (typeName) { | ||
generatedClass.push.apply(generatedClass, typeNames.map(function (typeName) { | ||
var type = ast.getTypeMap()[typeName]; | ||
return generator[type.constructor.name] ? generator[type.constructor.name](type) : null; | ||
}); | ||
})); | ||
// Special case 1: generate schema interface | ||
@@ -23,0 +25,0 @@ if (generator.SchemaType) { |
import { GraphQLObjectType, GraphQLUnionType, GraphQLInputObjectType, GraphQLScalarType, GraphQLEnumType, GraphQLInterfaceType } from "graphql"; | ||
export interface Generator { | ||
Main: (queryType: GraphQLObjectType, mutationType?: GraphQLObjectType | null, subscriptionType?: GraphQLObjectType | null) => string; | ||
Header: (schema: string) => string; | ||
SchemaType?: (queryType: GraphQLObjectType, mutationType?: GraphQLObjectType | null, subscriptionType?: GraphQLObjectType | null) => string; | ||
@@ -5,0 +6,0 @@ RootType?: (type: GraphQLObjectType) => string; |
{ | ||
"name": "graphql-static-binding", | ||
"version": "0.1.0", | ||
"version": "0.1.1", | ||
"description": "Generate static binding files for a GraphQL schema", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Long strings
Supply chain riskContains long string literals, which may be a sign of obfuscated or packed code.
Found 1 instance in 1 package
34477
47.94%208
7.77%16
-5.88%2
100%