Skip to Content

Configure GraphQL DataSource

Published: January 02, 2022

One of the core competencies of WunderGraph is GraphQL. You can introspect any GraphQL API and use it in your WunderGraph Application.

Once you have it introspected, pass it to the Application and you're good to go.

const countries = introspect.graphql({
apiNamespace: "countries",
url: "https://countries.trevorblades.com/",
headers: builder => builder
.addStaticHeader("StaticToken","secret") // inject a static token into all upstream requests
})

If you're looking into more details on configuring static and dynamic upstream headers, have a look at the dedicated configuration section on configuring Headers for HTTP-based APIs.

Skipping introspection / Manual introspection#

In some environments, it might not be possible or desired for you to introspect a GraphQL API. In these cases, it's possible to skip introspection and supply the GraphQL Schema manually.

You're able to import a Schema file and load the Schema from a string. Alternatively, you can use any other method to load the Schema as a string and provide it to the introspect.graphql function.

Setting the (optional) loadSchemaFromString field will skip the GraphQL introspection Query process and instead just rely on the supplied schema. Keep in mind that it's still required, and important, to set the URL of the GraphQL Endpoint, as this is not part of the GraphQL Schema.

The function loadSchemaFromString will introspect the supplied value. If it's a file import, the file will be resolved to a string. If it's a raw Schema string, it will be used as is.

import spacexSchema from "./spacex.graphql";
const spaceX = introspect.graphql({
apiNamespace: "spacex",
url: "https://api.spacex.land/graphql/",
loadSchemaFromString: spacexSchema,
});

Custom Scalars#

Some GraphQL servers use custom scalars, e.g. DateTime, UUID, float8 or BigInt. WunderGraph is designed to resolve GraphQL Operations in a secure way, meaning that we don't want to make assumptions about your Schema or allow "any" types.

For that reason, we're assuming that all custom scalars are of type string. This works for most cases, e.g. DateTime and UUID are of type string.

However, if your GraphQL server uses custom scalars that are not of type string, WunderGraph will not be able to resolve these scalars out of the box.

If you want to use custom scalars for Float or Int data types, you have to define them in the introspection configuration.

Here's an example configuration:

// wundergraph.config.ts
const apiWithCustomScalars = introspect.graphql({
apiNamespace: "custom",
url: "http://localhost:8111",
customFloatScalars: [
"float8",
"geography",
"geometry"
],
customIntScalars: [
"int8",
"int2",
"int4"
]
});

By adding this configuration, WunderGraph will automatically rename the scalars to Float and Int, making them compatible with the default JSON data types.


Product

Comparisons

Subscribe to our newsletter!

Stay informed when great things happen! Get the latest news about APIs, GraphQL and more straight into your mailbox.

© 2022 WunderGraph