-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(graphql): Generate fragment types from schema file
When providing Hops with a GraphQL schema file the command `hops graphql introspect` will try to generate the fragment types (required for querying fragments on unions or intersections) from the provided file. If this file does not exist, it will try to generate the fragment types by querying the GraphQL server specified via "graphqlUri".
- Loading branch information
1 parent
9d1dc74
commit 5d86841
Showing
4 changed files
with
121 additions
and
57 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 |
---|---|---|
@@ -1,49 +1,66 @@ | ||
/* global fetch */ | ||
'use strict'; | ||
|
||
require('isomorphic-fetch'); | ||
var fs = require('fs'); | ||
|
||
var fetch = require('isomorphic-fetch'); | ||
var pify = require('pify'); | ||
var graphql = require('graphql').graphql; | ||
var makeExecutableSchema = require('graphql-tools').makeExecutableSchema; | ||
|
||
var hopsConfig = require('hops-config'); | ||
|
||
var fragmentsFile = require('./util').getFragmentsFile(); | ||
|
||
module.exports = function fetchFragments() { | ||
return fetch(hopsConfig.graphqlUri, { | ||
function writeFragmentTypesFile(result) { | ||
result.data.__schema.types = result.data.__schema.types.filter(function(t) { | ||
return t.possibleTypes !== null; | ||
}); | ||
return pify(fs.writeFile)(fragmentsFile, JSON.stringify(result.data)); | ||
} | ||
|
||
function executeRemoteQuery(graphqlUri, query) { | ||
return fetch(graphqlUri, { | ||
method: 'POST', | ||
headers: { 'Content-Type': 'application/json' }, | ||
body: JSON.stringify({ | ||
query: [ | ||
/* eslint-disable indent */ | ||
' {', | ||
' __schema {', | ||
' types {', | ||
' kind', | ||
' name', | ||
' possibleTypes {', | ||
' name', | ||
' }', | ||
' }', | ||
' }', | ||
' }', | ||
/* eslint-enable indent */ | ||
].join('\n'), | ||
}), | ||
}) | ||
.then(result => result.json()) | ||
.then(result => { | ||
var filteredData = result.data.__schema.types.filter(function(type) { | ||
return type.possibleTypes !== null; | ||
}); | ||
result.data.__schema.types = filteredData; | ||
return new Promise(function(resolve, reject) { | ||
fs.writeFile(fragmentsFile, JSON.stringify(result.data), err => { | ||
if (err) { | ||
reject(err); | ||
} else { | ||
resolve(fragmentsFile); | ||
} | ||
}); | ||
}); | ||
body: JSON.stringify({ query: query }), | ||
}).then(function(result) { | ||
return result.json(); | ||
}); | ||
} | ||
|
||
function executeLocalQuery(schemaFile, query) { | ||
return pify(fs.readFile)(schemaFile, 'utf-8') | ||
.then(function(schema) { | ||
return makeExecutableSchema({ typeDefs: schema }); | ||
}) | ||
.then(function(executableSchema) { | ||
return graphql(executableSchema, query); | ||
}); | ||
} | ||
|
||
var query = [ | ||
'{', | ||
' __schema {', | ||
' types {', | ||
' kind', | ||
' name', | ||
' possibleTypes {', | ||
' name', | ||
' }', | ||
' }', | ||
' }', | ||
'}', | ||
].join('\n'); | ||
|
||
module.exports = function generateFragmentTypes() { | ||
return pify(fs.access)(hopsConfig.graphqlSchemaFile) | ||
.then( | ||
function() { | ||
return executeLocalQuery(hopsConfig.graphqlSchemaFile, query); | ||
}, | ||
function() { | ||
return executeRemoteQuery(hopsConfig.graphqlUri, query); | ||
} | ||
) | ||
.then(writeFragmentTypesFile); | ||
}; |
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