本文整理汇总了TypeScript中graphql.buildSchema函数的典型用法代码示例。如果您正苦于以下问题:TypeScript buildSchema函数的具体用法?TypeScript buildSchema怎么用?TypeScript buildSchema使用的例子?那么, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了buildSchema函数的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: valueToSchema
function valueToSchema(
schema: GraphQLSchema | string | Source | IntrospectionResult,
): GraphQLSchema {
if (schema instanceof GraphQLSchema) {
return schema
} else if (typeof schema === 'string') {
return buildSchema(schema)
} else if (schema instanceof Source) {
return buildSchema(schema)
} else if (typeof schema === 'object' && !Array.isArray(schema)) {
return introspectionToSchema(schema as IntrospectionResult)
}
throw new Error('Can not convert data to a schema')
}
示例2: it
it('should override the scope from the target type with that specified on a field', async () => {
const schema = buildSchema(`
type Query {
droid(id: ID!): Droid @cacheControl(scope: PRIVATE)
}
type Droid @cacheControl(maxAge: 60, scope: PUBLIC) {
id: ID!
name: String!
}
`);
const hints = await collectCacheControlHints(
schema,
`
query {
droid(id: 2001) {
name
}
}
`,
{ defaultMaxAge: 10 },
);
expect(hints).toContainEqual({
path: ['droid'],
maxAge: 60,
scope: CacheScope.Private,
});
});
示例3: graphQLTypes
export async function graphQLTypes(): Promise<void> {
const schemaStr = await readFile(GRAPHQL_SCHEMA_PATH, 'utf8')
const schema = buildSchema(schemaStr)
const result = (await graphql(schema, introspectionQuery)) as { data: IntrospectionQuery }
const formatOptions = (await resolveConfig(__dirname, { config: __dirname + '/../prettier.config.js' }))!
const typings =
'export type ID = string\n\n' +
generateNamespace(
'',
result,
{
typeMap: {
...DEFAULT_TYPE_MAP,
ID: 'ID',
},
},
{
generateNamespace: (name: string, interfaces: string) => interfaces,
interfaceBuilder: (name: string, body: string) =>
'export ' + DEFAULT_OPTIONS.interfaceBuilder(name, body),
enumTypeBuilder: (name: string, values: string) =>
'export ' + DEFAULT_OPTIONS.enumTypeBuilder(name, values),
typeBuilder: (name: string, body: string) => 'export ' + DEFAULT_OPTIONS.typeBuilder(name, body),
wrapList: (type: string) => `${type}[]`,
postProcessor: (code: string) => format(code, { ...formatOptions, parser: 'typescript' }),
}
)
await writeFile(__dirname + '/src/graphql/schema.ts', typings)
}
示例4: function
export const jsSchemaLoader: SchemaLoader = async function (options: TJsSchemaLoaderOptions) {
const schemaPath = resolve(options.schemaFile);
let schemaModule = require(schemaPath);
let schema: string;
// check if exist default in module
if (typeof schemaModule === 'object') {
schemaModule = schemaModule.default
}
// check for array of definition
if (Array.isArray(schemaModule)){
schema = schemaModule.join('');
// check for array array wrapped in a function
} else if (typeof schemaModule === 'function') {
schema = schemaModule().join('');
} else {
throw new Error(`Unexpected schema definition on "${schemaModule}", must be an array or function`)
}
const introspection = await execute(
buildSchema(schema),
parse(introspectionQuery)
) as Introspection;
return introspection.data.__schema;
};
示例5: test
test('typescript definition generator', t => {
const schema = buildSchema(typeDefs)
const generator = new JavascriptGenerator({
schema,
})
const javascript = generator.renderJavascript()
t.snapshot(javascript)
})
示例6: test
test('typescript generator', t => {
const schema = buildSchema(typeDefs)
const generator = new TypescriptGenerator({
schema,
})
const result = generator.render()
t.snapshot(result)
})
示例7: test
test('typescript generator - embedded', t => {
const schema = buildSchema(typeDefs)
const generator = new TypescriptGenerator({
schema,
internalTypes: parseInternalTypes(datamodel, DatabaseType.postgres).types,
})
const result = generator.render()
t.snapshot(result)
})