-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: refactor ctx.graphql query and mutate
- Loading branch information
1 parent
7a3679d
commit 78101c6
Showing
10 changed files
with
186 additions
and
47 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/test |
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,55 +1,41 @@ | ||
'use strict'; | ||
|
||
/** | ||
* copy from https://github.com/eggjs/egg-graphql/blob/master/app/service/graphql.js | ||
* 主要为了兼容egg-graphql | ||
*/ | ||
const { execute, formatError } = require('graphql'); | ||
const gql = require('graphql-tag'); | ||
const { createTestClient } = require('apollo-server-testing'); | ||
const GraphqlServer = Symbol.for('Egg#graphqlServer'); | ||
async function operate({ request, method }) { | ||
let result = {}; | ||
const graphqlServer = this.app[GraphqlServer]; | ||
const operation = createTestClient(graphqlServer)[method]; | ||
try { | ||
if (typeof request === 'string') { | ||
request = JSON.parse(request); | ||
} | ||
result = await operation(request); | ||
} catch (e) { | ||
this.logger.error(e); | ||
|
||
result = { | ||
data: {}, | ||
errors: [ e ], | ||
}; | ||
} | ||
|
||
return result; | ||
} | ||
module.exports = app => { | ||
class GraphqlService extends app.Service { | ||
|
||
async query(requestString) { | ||
let result = {}; | ||
const ctx = this.ctx; | ||
|
||
try { | ||
const params = JSON.parse(requestString); | ||
const { query, variables, operationName } = params; | ||
// GraphQL source. | ||
// https://github.com/apollostack/graphql-tag#caching-parse-results | ||
const documentAST = gql`${query}`; | ||
const context = ctx; | ||
const schema = this.app.schema; | ||
|
||
// http://graphql.org/graphql-js/execution/#execute | ||
result = await execute( | ||
schema, | ||
documentAST, | ||
null, | ||
context, | ||
variables, | ||
operationName | ||
); | ||
|
||
// Format any encountered errors. | ||
/* istanbul ignore if */ | ||
if (result && result.errors) { | ||
result.errors = result.errors.map(formatError); | ||
} | ||
} catch (e) { | ||
this.logger.error(e); | ||
|
||
result = { | ||
data: {}, | ||
errors: [ e ], | ||
}; | ||
} | ||
|
||
return result; | ||
async query(request) { | ||
return operate.call(this, { | ||
request, | ||
method: 'query', | ||
}); | ||
} | ||
async mutate(request) { | ||
return operate.call(this, { | ||
request, | ||
method: 'mutate', | ||
}); | ||
} | ||
} | ||
|
||
return GraphqlService; | ||
}; |
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
File renamed without changes
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
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.