graphql#assertValidSchema TypeScript Examples

The following examples show how to use graphql#assertValidSchema. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: schemaTask.ts    From genql with MIT License 5 votes vote down vote up
schemaTask = (config: Config): ListrTask => {
    const processSchema = (schema) => {
        if (config.sortProperties) {
            return lexicographicSortSchema(schema)
        }
        return schema
    }

    if (config.endpoint) {
        const endpoint = config.endpoint
        return {
            title: `fetching schema using ${
                config.useGet ? 'GET' : 'POST'
            } ${endpoint} and headers ${JSON.stringify(config.headers)}`,
            task: async (ctx) => {
                ctx.schema = processSchema(
                    await fetchSchema({
                        endpoint,
                        usePost: !config.useGet,
                        headers: config.headers,
                    }),
                )
            },
        }
    } else if (config.schema) {
        const schema = config.schema
        return {
            title: 'loading schema',
            task: async (ctx) => {
                // const options = config.options && config.options.schemaBuild
                const document = await loadSchema(schema, {
                    loaders: [new GraphQLFileLoader()],
                })
                ctx.schema = processSchema(document)

                try {
                    assertValidSchema(ctx.schema)
                } catch (e) {
                    if (e.message === 'Query root type must be provided.')
                        return
                    throw e
                }
            },
        }
    } else {
        throw new Error(
            'either `endpoint`, `fetcher` or `schema` must be defined in the config',
        )
    }
}