Installations
npm install @dreamit/graphql-server
Developer Guide
Typescript
Yes
Module System
ESM
Min. Node Version
>=v6.13.0
Node Version
20.18.1
NPM Version
10.8.2
Score
74.9
Supply Chain
98.4
Quality
92.8
Maintenance
100
Vulnerability
100
License
Releases
Contributors
Unable to fetch Contributors
Languages
TypeScript (100%)
Developer
dreamit-de
Download Statistics
Total Downloads
12,109
Last Day
53
Last Week
137
Last Month
472
Last Year
5,587
GitHub Statistics
4 Stars
821 Commits
2 Forks
3 Watching
10 Branches
6 Contributors
Bundle Size
21.97 kB
Minified
5.83 kB
Minified + Gzipped
Package Meta Information
Latest Version
4.12.2
Package Id
@dreamit/graphql-server@4.12.2
Unpacked Size
329.07 kB
Size
62.62 kB
File Count
11
NPM Version
10.8.2
Node Version
20.18.1
Publised On
13 Dec 2024
Total Downloads
Cumulative downloads
Total Downloads
12,109
Last day
1,225%
53
Compared to previous day
Last week
-32.5%
137
Compared to previous week
Last month
-24.7%
472
Compared to previous month
Last year
7.6%
5,587
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
Peer Dependencies
3
graphql-server
A GraphQL server implementation written in NodeJS/Typescript. It uses the standard graphql library to receive GraphQL requests and send back appropriate responses.
Installation
1npm install --save @dreamit/graphql-server
TypeScript declarations are provided within the project.
Compatibility
The following table shows which version of graphql-js, @dreamit/graphql-server-base and @dreamit/funpara are compatible with which version of
@dreamit/graphql-server
. As @dreamit/graphql-server
defines them as peerDependency you might want to
choose a fitting version used in your project and by other libraries depending
on them.
graphql-js version | graphql-server version | graphql-server-base version | funpara version | Github branch | Development Status |
---|---|---|---|---|---|
end of life | |||||
end of life | |||||
^16.0.0 | 3.x | ^1.0.1 | n.a. | legacy-server-v3 | maintenance |
^16.0.0 | 4.x | ^2.0 | ^1.0 | main | active |
Features
- Creates GraphQL responses
- Can be used with many webservers (see Webserver compatibility).
- Uses out-of-the-box default options to ease use and keep code short
- Provides hot reloading for schema and options
- Provides out-of-the-box metrics for GraphQLServer
- Uses only 3 peerDependencies: graphql-js version 16, graphql-server-base version 2 and funpara version 1 (no other production dependencies)
Handling and executing requests
GraphQLServer
provides the function handleRequest
to handle and execute requests.
Depending on the provided parameters different actions will be executed in order to send or return the ExecutionResult
- request: If the request is a
GraphQLServerRequest
theextractInformationFromRequest
function will be used to extract information from the request (url and/or body) and be available asGraphQLRequestInfo
. If the request already is aGraphQLRequestInfo
this information will be used without extracting information from the server request. - response: If a response is provided (i.e. not undefined), a response will be sent using
sendResponse
function and theGraphQLExecutionResult
will be returned. If response is undefined, no response will be sent and theGraphQLExecutionResult
will be returned.
1class GraphQLServer { 2 async handleRequest( 3 request: GraphQLServerRequest | GraphQLRequestInfo, 4 response?: GraphQLServerResponse, 5 ): Promise<GraphQLExecutionResult> {} 6}
Use cases
The handleRequest
function can be used for many use cases. The following part lists some use cases with a short
description. It is possible to use handleRequest
with different parameters with a single GraphQLServer
instance,
e.g. when using a webserver with websockets or messaging.
handleRequest
withGraphQLServerRequest
andGraphQLServerResponse
: Use as webserver middleware. Create an instance ofGraphQLServer
and use the request and response provided by the webserver as parameters. You might need to wrap one or both values, see Webserver compatibilityhandleRequest
withGraphQLRequestInfo
: Use for flexible GraphQL execution, e.g. for websockets or messaging. Create an instance ofGraphQLServer
and given aGraphQLRequestInfo
the request can be executed and the returnedGraphQLExecutionResult
can be used for multiple purposes like sending a message or responding to a websocket request.handleRequest
withGraphQLServerRequest
: Use as alternative webserver middleware or if custom actions should be done before sending back a response. Create an instance ofGraphQLServer
and use the request provided by the webserver as parameter for this function. You might need request values, see Webserver compatibility. The returnedGraphQLExecutionResult
can be used to execute custom logic with the result and/or prepare or send a response.handleRequest
withGraphQLRequestInfo
andGraphQLServerResponse
: Use if aGraphQLRequestInfo
is available and a response should be sent from this request.
Usage as webserver middleware
You can create a new instance of GraphQLServer
with the options necessary for your tasks. The
handleRequest
function of the GraphQLServer
can be integrated with many fitting webservers.
Note regarding POST requests:
graphql-server version 3 and higher try to extract the request information from the request.body
field. Some webserver
frameworks like Express might need a fitting body parser in order to populate this body
field.
- parse body as
string/text
(recommended): graphql-server will handle reading content and parsing it to JSON. - parse body as
object/JSON
: graphql-server will read JSON and try to assign it to matching fields. This might cause FetchErrors if the body contains invalid JSON. We recommend using text parsers instead so graphql-server can respond with a fitting GraphQL error response if JSON is invalid.
1const graphQLServerPort = 3592 2const graphQLServerExpress = express() 3const customGraphQLServer = new GraphQLServer({ schema: someExampleSchema }) 4graphQLServerExpress.use(bodyParser.text({ type: '*/*' })) 5graphQLServerExpress.all('/graphql', (req, res) => { 6 return customGraphQLServer.handleRequest(req, res) 7}) 8graphQLServerExpress.listen({ port: graphQLServerPort }) 9console.info(`Starting GraphQL server on port ${graphQLServerPort}`)
GraphQLServer
provides default values and behaviour out of the box. It is recommended to at least provide a schema
so the request won't be rejected because of a missing/invalid schema. When using it with a local schema it is
recommended to provide a rootValue
to return a fitting value. Examples for these requests can be found in the
integration test in the GraphQLServer.integration.test.ts
class in the tests/server
folder.
Schema validation and disabling Introspection
Validation rules can be used to define how the GraphQLServer
should behave when validating the request against the
given schema. To ease the use GraphQLServer
uses the specifiedRules
from graphql-js library. If you don't want
to use the default validation rules you can overwrite them by setting defaultValidationRules
option to []
.
Warning!
Setting both defaultValidationRules
and customValidationRules
options to []
will disable validation. This might
result in unexpected responses that are hard to use for API users or frontends.
1import { NoSchemaIntrospectionCustomRule } from 'graphql' 2 3const graphQLServerPort = 3592 4const graphQLServerExpress = express() 5const customGraphQLServer = new GraphQLServer({ 6 schema: someExampleSchema, 7 defaultValidationRules: [], 8}) 9graphQLServerExpress.use(bodyParser.text({ type: '*/*' })) 10graphQLServerExpress.all('/graphql', (req, res) => { 11 return customGraphQLServer.handleRequest(req, res) 12}) 13graphQLServerExpress.listen({ port: graphQLServerPort }) 14console.info(`Starting GraphQL server on port ${graphQLServerPort}`)
If you want to define custom validation rules you can use the customValidationRules
option (e.g. to handle
introspection like shown in the example below).
Introspection can be used to get information about the available schema. While this may be useful in development environments and public APIs you should consider disabling it for production if e.g. your API is only used with a specific matching frontend.
Introspection can be disabled by adding the NoSchemaIntrospectionCustomRule
from the graphql-js library to the
customValidationRules
option.
1import { NoSchemaIntrospectionCustomRule } from 'graphql' 2 3const graphQLServerPort = 3592 4const graphQLServerExpress = express() 5const customGraphQLServer = new GraphQLServer({ 6 schema: someExampleSchema, 7 customValidationRules: [NoSchemaIntrospectionCustomRule], 8}) 9graphQLServerExpress.use(bodyParser.text({ type: '*/*' })) 10graphQLServerExpress.all('/graphql', (req, res) => { 11 return customGraphQLServer.handleRequest(req, res) 12}) 13graphQLServerExpress.listen({ port: graphQLServerPort }) 14console.info(`Starting GraphQL server on port ${graphQLServerPort}`)
Schema hot reload
Hot reload of the GraphQL schema can be used to update the existing schema to a new version without restarting the GraphQL server, webserver or whole application. When setting a new schema it will be used for the next incoming request while the old schema will be used for requests that are being processed at the moment. Hot reloading is especially useful for remote schemas that are processed in another application like a webservice.
The schema can be changed simply by calling setSchema
in the GraphQLServer
instance. In the example below a second
route is used to trigger a schema update.
1const graphQLServerPort = 3592 2const graphQLServerExpress = express() 3const customGraphQLServer = new GraphQLServer({ schema: someExampleSchema }) 4graphQLServerExpress.use(bodyParser.text({ type: '*/*' })) 5graphQLServerExpress.all('/graphql', (req, res) => { 6 return customGraphQLServer.handleRequest(req, res) 7}) 8graphQLServerExpress.all('/updateme', (req, res) => { 9 const updatedSchema = someMagicHappened() 10 customGraphQLServer.setSchema(updatedSchema) 11 return res.status(200).send() 12}) 13graphQLServerExpress.listen({ port: graphQLServerPort }) 14console.info(`Starting GraphQL server on port ${graphQLServerPort}`)
Metrics
There are 2 builtin MetricsClient
implementations available.
- SimpleMetricsClient: Used as default
MetricsClient
. Provides GraphQLServer related metrics without but does not provide NodeJS metrics like cpu and memory usage. - NoMetricsClient: Does not collect any metrics. Can be used to disable metrics collection/increase performance.
The SimpleMetricsClient provides three custom metrics for the GraphQL server:
- graphql_server_availability: Availability gauge with status 0 (unavailable) and 1 (available)
- graphql_server_request_throughput: The number of incoming requests
- graphql_server_errors: The number of errors that are encountered while running the GraphQLServer. The counter uses
the errorName field as label so errors could be differentiated. At the moment the following labels are available and
initialized with 0:
- FetchError
- GraphQLError
- SchemaValidationError
- MethodNotAllowedError
- InvalidSchemaError
- MissingQueryParameterError
- ValidationError
- SyntaxError
- IntrospectionDisabledError
A simple metrics endpoint can be created by using getMetricsContentType
and getMetrics
functions from
the GraphQLServer
instance. In the example below a second route is used to return metrics data.
1const graphQLServerPort = 3592 2const graphQLServerExpress = express() 3const customGraphQLServer = new GraphQLServer({ schema: someExampleSchema }) 4graphQLServerExpress.use(bodyParser.text({ type: '*/*' })) 5graphQLServerExpress.all('/graphql', (req, res) => { 6 return customGraphQLServer.handleRequest(req, res) 7}) 8graphQLServerExpress.get('/metrics', async (req, res) => { 9 return res 10 .contentType(customGraphQLServer.getMetricsContentType()) 11 .send(await customGraphQLServer.getMetrics()) 12}) 13graphQLServerExpress.listen({ port: graphQLServerPort }) 14console.info(`Starting GraphQL server on port ${graphQLServerPort}`)
CORS requests
The GraphQLServer
does not handle CORS requests on its own. It is recommended to handle this on the webserver level,
e.g. by using cors
library with an Express webserver like in the example below.
1const graphQLServerPort = 3592 2const graphQLServerExpress = express() 3graphQLServerExpress.use(cors()) 4const customGraphQLServer = new GraphQLServer({ schema: someExampleSchema }) 5graphQLServerExpress.use(bodyParser.text({ type: '*/*' })) 6graphQLServerExpress.all('/graphql', (req, res) => { 7 return customGraphQLServer.handleRequest(req, res) 8}) 9graphQLServerExpress.listen({ port: graphQLServerPort }) 10console.info(`Starting GraphQL server on port ${graphQLServerPort}`)
Webserver compatibility
The handleRequest
function works with webservers that provide a fitting request and
response object that matches GraphQLServerRequest
and GraphQLServerResponse
interface. As Express (since
version 2.x) matches both no further adjustment is necessary. If one or both objects do not match GraphQLServerRequest
and GraphQLServerResponse
it might still be possible to map the webserver request and response objects to these interfaces.
In the following table a list of webserver frameworks/versions can be found that are able to run GraphQLServer
.
The Version
column shows the version of the webserver framework we tested GraphQLServer
version 4 with.
If the request and/or response has to be mapped it is noted in the Mapping
column. There is a code
examples on how to use handleRequest
without providing a GraphQLServerResponse
and sending the response with the
functionality provided by the webserver.
Framework/Module | Version | Mapping | Example |
---|---|---|---|
AdonisJS | 6.3 | request, response | AdonisJS example |
Express | > = 2.x | none | Express example |
fastify | 4.23 | response | Fastify example |
hapi | 21.3.7 | request, no response | hapi example |
Koa | 2.15.2 | response | Koa example |
Next.js | 14.1.4 | none | Next.js example |
Nitro | 4.9.5 | request | Nitro example |
NodeJS http | 20.x | request | NodeJS http example |
Socket.IO | 4.7.5 | response | Socket.IO example |
gRPC | 1.8.14 | no response | gRPC example |
Deno | 1.42.1 | request, no response | Deno HTTP example |
Bun | 1.1.0 | request, no response | Bun example |
GraphQLServerRequest
and GraphQLServerResponse
interfaces
The GraphQLServerRequest
and GraphQLServerResponse
are available in the @dreamit/graphql-server-base module.
This allows extensions such as custom Logger
or MetricsClient
implementations to implement these interfaces without
defining @dreamit/graphql-server
as dependency.
1export interface GraphQLServerRequest { 2 headers: IncomingHttpHeaders 3 url?: string 4 body?: unknown 5 method?: string 6} 7 8export interface GraphQLServerResponse { 9 statusCode: number 10 setHeader( 11 name: string, 12 value: number | string | ReadonlyArray<string>, 13 ): this 14 end(chunk: unknown, callback?: () => void): this 15 removeHeader(name: string): void 16}
Working with context function
GraphQLServer
, like many GraphQL libraries, uses a context function to create a context object that is available
during the whole request execution process. This can for example be used to inject information about request headers
or adjust responses. An example can be found in the CustomSendResponse.integration.test.ts
class in the test/server folder.
1export interface GraphQLServerOptions { 2 readonly contextFunction?: (contextParameters: { 3 serverOptions: GraphQLServerOptions 4 request?: GraphQLServerRequest 5 response?: GraphQLServerResponse 6 }) => unknown 7}
Available options
The GraphQLServer
accepts the following options. Note that all options are optional and can be overwritten by calling
the setOptions
function of the GraphQLServer
instance.
GraphQL related options
schema
: The schema that is used to handle the request and send a response. If undefined theGraphQLServer
will reject responses with a GraphQL error response with status code 500.shouldUpdateSchemaFunction
: Function that can be used to determine whether a schema update should be executed.formatErrorFunction
: Function that can be used to format occurring GraphQL errors. Given aGraphQLError
it should return aGraphQLFormattedError
. By defaultdefaultFormatErrorFunction
is called that useserror.toJSON
to format the error.schemaValidationFunction
: Function that is called when a schema is set or updated. Given aGraphQLSchema
it can return aReadonlyArray<GraphQLError>
or an empty array if no errors occurred/should be returned. By defaultvalidateSchema
from graphql-js library is called.parseFunction
: Function that is called to create aDocumentNode
with the extracted query in the request information. Given asource
andParseOptions
it should return aDocumentNode
. By defaultparse
from graphql-js library is called.defaultValidationRules
: Default validation rules that are used whenvalidateSchemaFunction
is called. BothdefaultValidationRules
andcustomValidationRules
will be merged together whenvalidateSchemaFunction
is called. By defaultspecifiedRules
from graphql-js are used. Can be overwritten if no or other default rules should be used.customValidationRules
: Custom validation rules that are used whenvalidateSchemaFunction
is called. BothdefaultValidationRules
andcustomValidationRules
will be merged together whenvalidateSchemaFunction
is called. By default, an empty array is set. Can be overwritten to add additional rules likeNoSchemaIntrospectionCustomRule
.validationTypeInfo
: Validation type info that is used whenvalidateSchemaFunction
is called.validationOptions
: Validation options containing{ maxErrors?: number }
that is used whenvalidateSchemaFunction
is called.removeValidationRecommendations
: Iftrue
removes validation recommendations like "users not found. Did you mean user?". For non-production environments it is usually safe to allow recommendations. For production environments when not providing access to third-party users it is considered good practice to remove these recommendations so users can not circumvent disabled introspection request by using recommendations to explore the schema.validateFunction
: Validation function that validates the extracted request against the available schema. By defaultvalidate
from graphql-js library is called.rootValue
: Root value that is used whenexecuteFunction
is called. Can be used to define resolvers that handle how defined queries and/or mutations should be resolved (e.g. fetch object from database and return entity).fieldResolver
: Field resolver function that is used whenexecuteFunction
is called. Default is undefined, if custom logic is necessary it can be added.typeResolver
: Type resolver function that is used whenexecuteFunction
is called. Default is undefined, if custom logic is necessary it can be added.executeFunction
: Execute function that executes the parsedDocumentNode
(created inparseFunction
) using given schema, values and resolvers. Returns a Promise or value of anExecutionResult
. By defaultexecute
from graphql-js library is called.extensionFunction
: Extension function that can be used to add additional information to theextensions
field of the response. Given aGraphQLRequestInfo
,ExecutionResult
,GraphQLServerOptions
and context it should return undefined or an ObjMap of key-value-pairs that are added to theextensions
field. By defaultdefaultExtensions
is used and returns undefined.reassignAggregateError
: Iftrue
and theExecutionResult
created by theexecuteFunction
contains anAggregateError
(e.g. an error containing a comma-separated list of errors in the message and anoriginalError
containing multiple errors) this function will reassign theoriginalError.errors
to theExecutionResult.errors
field. This is helpful if another application createsAggregateErrors
while the initiator of the request (e.g. a Frontend app) does not expect or know how to handleAggregateErrors
.
Context function
contextFunction
: GivenGraphQLServerOptions
,GraphQLServerRequest
andGraphQLServerResponse
this function is used to create a context value that is available in the whole request flow. Default implementation isdefaultContextFunction
that returns the givenGraphQLServerRequest
. Can be used to extract information from the request and/or response and return them as context. This is often used to extract headers like 'Authorization' and set them in the execute function.
Error messages
executionResultErrorMessage:
: Error message that is used in logging if a response contains anerrors
element.fetchErrorMessage:
: If provided and not set to undefined, used as fixed error message if a FetchError occurs.graphqlExecutionErrorMessage:
: Error message that is used in logging if an error is thrown whenexecute
function is called.validationErrorMessage:
: Error message that is used in logging if one or more errors occurred when calling thevalidate
function.
Error responses
methodNotAllowedResponse:
: Function given a method asstring
returns an error that the used method is not allowed byGraphQLServer
.invalidSchemaResponse:
: Default error that is returned with set schema is invalid.missingQueryParameterResponse:
: Default error that is returned if no query is available in theGraphQLRequestInfo
.onlyQueryInGetRequestsResponse:
: Function given an operation asstring
returns an error that the used operation is not allowed forGET
requests.
Metrics options
collectErrorMetricsFunction:
: Given an error name as string, error asunknown
,GraphQLServerOptions
and context asunknown
this function can be used to trigger collecting error metrics. Default implementation isdefaultCollectErrorMetrics
that increase the error counter for the given errorName or Error by 1.
Technical components
logger
: Logger to be used in the GraphQL server.TextLogger
andJsonLogger
as well asNoStacktraceTextLogger
andNoStacktraceJsonLogger
(useful for tests without the need for a stacktrace) andNoLogger
(useful if no logging should be done but logger is required) are available in the module. Own Logger can be created by implementingLogger
interface.extractInformationFromRequest
: Function that can be used to extract information from theGraphQLServerRequest
and return aPromise<GraphQLRequestInfo>
. By default, theextractInformationFromRequest
function is used that tries to extract the information from the body (usingrequest.body
field) and URL params of the request.sendResponse
: Function used to send a fitting response being either adata
orerror
response. By default, thesendResponse
is used that tries to create and send a response using the functions provided by the givenGraphQLServerResponse
.metricsClient
: TheMetricsClient
used to collect metrics from the GraphQLServer. By default, theSimpleMetricsClient
is used that collects three custom metrics. Own MetricsClient can be used by implementingMetricsClient
interface.responseEndChunkFunction
: Function used to adjust the chunk/body before it is used in theresponse.end
function call in thesendResponse
function. By default it stringifies the ExecutionResult and creates a Buffer from this string.adjustGraphQLExecutionResult
: Function used to adjust theGraphQLExecutionResult
before it is returned.
Customize and extend GraphQLServer
To make it easier to customize and extend the GraphQLServer
classes and class functions are public. This makes extending
a class and overwriting logic easy.
In the example below the logic of TextLogger
is changed to add the text "SECRETAPP" in front of every log output.
1export class SecretApplicationTextLogger extends TextLogger { 2 prepareLogOutput(logEntry: LogEntry): string { 3 return `SECRETAPP - ${super.prepareLogOutput(logEntry)}` 4 } 5}
Contact
If you have questions or issues please visit our Issue page and open a new issue if there are no fitting issues for your topic yet.
License
graphql-server is under MIT-License.
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
13 out of 13 merged PRs checked by a CI test -- score normalized to 10
Reason
no dangerous workflow patterns detected
Reason
update tool detected
Details
- Info: detected update tool: Dependabot: .github/dependabot.yml:1
Reason
project is fuzzed
Details
- Info: TypeScriptPropertyBasedTesting integration found: tests/request/ExtractInformationFromRequest.test.ts:1
- Info: TypeScriptPropertyBasedTesting integration found: tests/response/SendResponse.test.ts:1
Reason
license file detected
Details
- Info: project has a license file: LICENSE:0
- Info: FSF or OSI recognized license: MIT License: LICENSE:0
Reason
30 commit(s) and 5 issue activity found in the last 90 days -- score normalized to 10
Reason
all dependencies are pinned
Details
- Info: 11 out of 11 GitHub-owned GitHubAction dependencies pinned
- Info: 7 out of 7 third-party GitHubAction dependencies pinned
- Info: 4 out of 4 npmCommand dependencies pinned
Reason
SAST tool is run on all commits
Details
- Info: all commits (30) are checked with a SAST tool
Reason
GitHub workflow tokens follow principle of least privilege
Details
- Warn: jobLevel 'contents' permission set to 'write': .github/workflows/automerge.yml:43
- Warn: jobLevel 'actions' permission set to 'write': .github/workflows/automerge.yml:45
- Info: topLevel 'contents' permission set to 'read': .github/workflows/automerge.yml:7
- Info: topLevel 'contents' permission set to 'read': .github/workflows/ci.yaml:5
- Info: topLevel 'contents' permission set to 'read': .github/workflows/publish.yaml:11
- Info: topLevel permissions set to 'read-all': .github/workflows/scorecard.yml:18
Reason
0 existing vulnerabilities detected
Reason
branch protection is not maximal on development and all release branches
Details
- Info: 'allow deletion' disabled on branch 'main'
- Info: 'force pushes' disabled on branch 'main'
- Info: 'branch protection settings apply to administrators' is required to merge on branch 'main'
- Info: 'stale review dismissal' is required to merge on branch 'main'
- Warn: branch 'main' does not require approvers
- Warn: codeowners review is not required on branch 'main'
- Warn: 'last push approval' is disable on branch 'main'
- Info: 'up-to-date branches' is required to merge on branch 'main'
- Info: status check found to merge onto on branch 'main'
- Info: PRs are required in order to make changes on branch 'main'
Reason
project has 1 contributing companies or organizations -- score normalized to 3
Details
- Info: dreamit-de contributor org/company found,
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
Found 0/3 approved changesets -- score normalized to 0
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Score
7.7
/10
Last Scanned on 2024-12-17T17:37:19Z
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More