forked from open-telemetry/opentelemetry-js
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: GraphQL throws TypeError: Cannot read property 'startToken' of u…
…ndefined (open-telemetry#619) Co-authored-by: Daniel Dyla <dyladan@users.noreply.github.com>
- Loading branch information
Showing
7 changed files
with
157 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
'use strict'; | ||
|
||
const url = require('url'); | ||
const http = require('http'); | ||
// Construct a schema, using GraphQL schema language | ||
|
||
const source = ` | ||
{ | ||
continents { | ||
code | ||
name | ||
} | ||
} | ||
`; | ||
|
||
makeRequest(source).then(console.log); | ||
|
||
function makeRequest(query) { | ||
return new Promise((resolve, reject) => { | ||
const parsedUrl = new url.URL('http://localhost:4000/graphql'); | ||
const options = { | ||
hostname: parsedUrl.hostname, | ||
port: parsedUrl.port, | ||
path: parsedUrl.pathname, | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
}, | ||
}; | ||
const req = http.request(options, (res) => { | ||
const data = []; | ||
res.on('data', (chunk) => data.push(chunk)); | ||
res.on('end', () => { | ||
resolve(data.toString()); | ||
}); | ||
res.on('error', (err) => { | ||
reject(err); | ||
}); | ||
}); | ||
|
||
req.write(JSON.stringify({ query })); | ||
req.end(); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
'use strict'; | ||
|
||
const { fetch } = require('cross-fetch'); | ||
const { print } = require('graphql'); | ||
const { wrapSchema, introspectSchema } = require('@graphql-tools/wrap'); | ||
const { transformSchemaFederation } = require('graphql-transform-federation'); | ||
|
||
const executor = async ({ document, variables }) => { | ||
const query = print(document); | ||
const fetchResult = await fetch('https://countries.trevorblades.com/', { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
}, | ||
body: JSON.stringify({ query, variables }), | ||
}); | ||
return fetchResult.json(); | ||
}; | ||
|
||
module.exports = async () => { | ||
const schema = wrapSchema({ | ||
schema: await introspectSchema(executor), | ||
executor, | ||
}); | ||
|
||
return transformSchemaFederation(schema, { | ||
Query: { | ||
extend: true, | ||
}, | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
'use strict'; | ||
|
||
require('./tracer'); | ||
|
||
const { ApolloServer } = require('apollo-server'); | ||
const { | ||
ApolloGateway, | ||
RemoteGraphQLDataSource, | ||
LocalGraphQLDataSource, | ||
} = require('@apollo/gateway'); | ||
|
||
const getCountriesSchema = require('./countries-service'); | ||
|
||
const setupGateway = async () => { | ||
const countriesSchema = await getCountriesSchema(); | ||
|
||
const gateway = new ApolloGateway({ | ||
serviceList: [{ name: 'countries', url: 'http://countries' }], | ||
|
||
// Experimental: Enabling this enables the query plan view in Playground. | ||
__exposeQueryPlanExperimental: false, | ||
|
||
buildService: ({ url }) => { | ||
if (url === 'http://countries') { | ||
return new LocalGraphQLDataSource(countriesSchema); | ||
} | ||
return new RemoteGraphQLDataSource({ | ||
url, | ||
}); | ||
}, | ||
}); | ||
|
||
return gateway; | ||
}; | ||
|
||
(async () => { | ||
const gateway = await setupGateway(); | ||
|
||
const server = new ApolloServer({ | ||
gateway, | ||
|
||
// Apollo Graph Manager (previously known as Apollo Engine) | ||
// When enabled and an `ENGINE_API_KEY` is set in the environment, | ||
// provides metrics, schema management and trace reporting. | ||
engine: false, | ||
|
||
// Subscriptions are unsupported but planned for a future Gateway version. | ||
subscriptions: false, | ||
}); | ||
|
||
server.listen().then(({ url }) => { | ||
console.log(`🚀 Server ready at ${url}`); | ||
}); | ||
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters