-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat:pass other apollo server options. (#31)
- Loading branch information
Showing
23 changed files
with
361 additions
and
8 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
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,35 @@ | ||
'use strict'; | ||
|
||
const assert = require('assert'); | ||
const mm = require('egg-mock'); | ||
|
||
describe('test/app/graphql-options.test.js', () => { | ||
let app; | ||
|
||
before(() => { | ||
app = mm.app({ | ||
baseDir: 'apps/graphql-options-app', | ||
}); | ||
return app.ready(); | ||
}); | ||
|
||
after(mm.restore); | ||
|
||
it('should return custom error, use formatError', async () => { | ||
const resp = await app.httpRequest() | ||
.get('/graphql?query=query+getUser($id:Int){user(id:$id){name}}&variables={"id":1}') | ||
.expect(200); | ||
assert.equal(resp.body.errors[0].code, 100001); | ||
}); | ||
|
||
it('should return frameworks, user formatResponse', async () => { | ||
const resp = await app.httpRequest() | ||
.get('/graphql?query=query+getFramework($id:Int){framework(id:$id){name}}&variables={"id":1}') | ||
.expect(200); | ||
assert.deepEqual(resp.body.data, { | ||
frameworks: { | ||
name: 'framework1', | ||
}, | ||
}); | ||
}); | ||
}); |
5 changes: 5 additions & 0 deletions
5
test/fixtures/apps/graphql-options-app/app/extend/application.js
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,5 @@ | ||
'use strict'; | ||
|
||
module.exports = { | ||
changedName: 'frameworks', | ||
}; |
12 changes: 12 additions & 0 deletions
12
test/fixtures/apps/graphql-options-app/app/graphql/directives/directive.js
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,12 @@ | ||
'use strict'; | ||
|
||
module.exports = { | ||
upper(next) { | ||
return next().then(str => { | ||
if (typeof str === 'string') { | ||
return str.toUpperCase(); | ||
} | ||
return str; | ||
}); | ||
}, | ||
}; |
1 change: 1 addition & 0 deletions
1
test/fixtures/apps/graphql-options-app/app/graphql/directives/schema.graphql
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 @@ | ||
directive @upper on FIELD_DEFINITION |
30 changes: 30 additions & 0 deletions
30
test/fixtures/apps/graphql-options-app/app/graphql/group/framework/connector.js
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,30 @@ | ||
'use strict'; | ||
|
||
const DataLoader = require('dataloader'); | ||
|
||
class FrameworkConnector { | ||
constructor(ctx) { | ||
this.ctx = ctx; | ||
this.loader = new DataLoader(this.fetch.bind(this)); | ||
} | ||
|
||
fetch(ids) { | ||
return Promise.resolve(ids.map(id => ({ | ||
id, | ||
name: `framework${id}`, | ||
projects: [], | ||
}))); | ||
} | ||
|
||
fetchByIds(ids) { | ||
return this.loader.loadMany(ids); | ||
} | ||
|
||
fetchById(id) { | ||
return this.loader.load(id); | ||
} | ||
|
||
} | ||
|
||
module.exports = FrameworkConnector; | ||
|
9 changes: 9 additions & 0 deletions
9
test/fixtures/apps/graphql-options-app/app/graphql/group/framework/resolver.js
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,9 @@ | ||
'use strict'; | ||
|
||
module.exports = { | ||
Query: { | ||
framework(root, { id }, ctx) { | ||
return ctx.connector.framework.fetchById(id); | ||
}, | ||
}, | ||
}; |
6 changes: 6 additions & 0 deletions
6
test/fixtures/apps/graphql-options-app/app/graphql/group/framework/schema.graphql
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,6 @@ | ||
|
||
type Framework { | ||
id: Int! | ||
name: String! | ||
projects: [Project] | ||
} |
12 changes: 12 additions & 0 deletions
12
test/fixtures/apps/graphql-options-app/app/graphql/project/resolver.js
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,12 @@ | ||
'use strict'; | ||
|
||
module.exports = app => { | ||
return { | ||
Query: { | ||
projects() { | ||
console.log(app); | ||
return []; | ||
}, | ||
}, | ||
}; | ||
}; |
4 changes: 4 additions & 0 deletions
4
test/fixtures/apps/graphql-options-app/app/graphql/project/schema.graphql
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,4 @@ | ||
|
||
type Project { | ||
name: String! | ||
} |
1 change: 1 addition & 0 deletions
1
test/fixtures/apps/graphql-options-app/app/graphql/schemaDirectives/schema.graphql
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 @@ | ||
directive @lowerCase on FIELD_DEFINITION |
21 changes: 21 additions & 0 deletions
21
test/fixtures/apps/graphql-options-app/app/graphql/schemaDirectives/schemaDirective.js
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,21 @@ | ||
'use strict'; | ||
|
||
const { SchemaDirectiveVisitor } = require('graphql-tools'); | ||
const { defaultFieldResolver } = require('graphql'); | ||
|
||
class LowerCaseDirective extends SchemaDirectiveVisitor { | ||
visitFieldDefinition(field) { | ||
const { resolve = defaultFieldResolver } = field; | ||
field.resolve = async function(...args) { | ||
let result = await resolve.apply(this, args); | ||
if (typeof result === 'string') { | ||
result = result.toLowerCase(); | ||
} | ||
return result; | ||
}; | ||
} | ||
} | ||
|
||
module.exports = { | ||
lowerCase: LowerCaseDirective, | ||
}; |
38 changes: 38 additions & 0 deletions
38
test/fixtures/apps/graphql-options-app/app/graphql/user/connector.js
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,38 @@ | ||
'use strict'; | ||
|
||
const DataLoader = require('dataloader'); | ||
|
||
class UserConnector { | ||
constructor(ctx) { | ||
this.ctx = ctx; | ||
this.loader = new DataLoader(this.fetch.bind(this)); | ||
} | ||
|
||
fetch(ids) { | ||
// this.ctx.model.user.find(ids); | ||
return Promise.resolve(ids.map(id => ({ | ||
id, | ||
name: `name${id}`, | ||
upperName: `name${id}`, | ||
lowerName: `name${id}`, | ||
password: `password${id}`, | ||
projects: [], | ||
}))); | ||
} | ||
|
||
fetchByIds(ids) { | ||
return this.loader.loadMany(ids); | ||
} | ||
|
||
// eslint-disable-next-line no-unused-vars | ||
fetchById(id) { | ||
const err = new Error(); | ||
err.code = 100001; | ||
throw err; | ||
// return this.loader.load(id); | ||
} | ||
|
||
} | ||
|
||
module.exports = UserConnector; | ||
|
9 changes: 9 additions & 0 deletions
9
test/fixtures/apps/graphql-options-app/app/graphql/user/resolver.js
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,9 @@ | ||
'use strict'; | ||
|
||
module.exports = { | ||
Query: { | ||
user(root, { id }, ctx) { | ||
return ctx.connector.user.fetchById(id); | ||
}, | ||
}, | ||
}; |
14 changes: 14 additions & 0 deletions
14
test/fixtures/apps/graphql-options-app/app/graphql/user/schema.graphql
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,14 @@ | ||
type Query { | ||
user(id: Int): User | ||
projects: [Project!] | ||
framework(id: Int): Framework | ||
} | ||
|
||
type User { | ||
id: String! | ||
password: String! | ||
name: String! | ||
upperName: String @upper | ||
lowerName: String @lowerCase | ||
projects: [Project!] | ||
} |
14 changes: 14 additions & 0 deletions
14
test/fixtures/apps/graphql-options-app/app/model/framework.js
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,14 @@ | ||
'use strict'; | ||
|
||
module.exports = () => { | ||
class Framework { | ||
find(ids) { | ||
return ids.map(id => ({ | ||
id, | ||
name: `name${id}`, | ||
})); | ||
} | ||
} | ||
|
||
return Framework; | ||
}; |
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,15 @@ | ||
'use strict'; | ||
|
||
module.exports = () => { | ||
class User { | ||
find(ids) { | ||
return ids.map(id => ({ | ||
id, | ||
name: `name${id}`, | ||
password: `password${id}`, | ||
})); | ||
} | ||
} | ||
|
||
return User; | ||
}; |
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,25 @@ | ||
'use strict'; | ||
|
||
module.exports = function(app) { | ||
app.get('/user', async ctx => { | ||
const req = { | ||
query: `{ | ||
user(id: 2) { | ||
name | ||
} | ||
}`, | ||
}; | ||
ctx.body = await ctx.graphql.query(JSON.stringify(req)); | ||
}); | ||
|
||
app.get('/framework', async ctx => { | ||
const req = { | ||
query: `{ | ||
framework(id: 2) { | ||
name | ||
} | ||
}`, | ||
}; | ||
ctx.body = await ctx.graphql.query(JSON.stringify(req)); | ||
}); | ||
}; |
13 changes: 13 additions & 0 deletions
13
test/fixtures/apps/graphql-options-app/app/service/framework.js
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,13 @@ | ||
'use strict'; | ||
|
||
module.exports = app => { | ||
class FrameworkService extends app.Service { | ||
async getFrameworkList() { | ||
return [ | ||
{ id: 1, name: 'framework1' }, | ||
{ id: 2, name: 'framework2' }, | ||
]; | ||
} | ||
} | ||
return FrameworkService; | ||
}; |
13 changes: 13 additions & 0 deletions
13
test/fixtures/apps/graphql-options-app/app/service/user.js
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,13 @@ | ||
'use strict'; | ||
|
||
module.exports = app => { | ||
class UserService extends app.Service { | ||
async getUserList() { | ||
return [ | ||
{ id: '1', name: 'user1' }, | ||
{ id: '2', name: 'user2' }, | ||
]; | ||
} | ||
} | ||
return UserService; | ||
}; |
Oops, something went wrong.