apollo-codegen
Advanced tools
Comparing version 0.13.2 to 0.14.0
{ | ||
"name": "apollo-codegen", | ||
"version": "0.13.2", | ||
"version": "0.14.0", | ||
"description": "Generate API code or type annotations based on a GraphQL schema and query documents", | ||
@@ -29,3 +29,3 @@ "main": "./lib/index.js", | ||
"@types/node-fetch": "^1.6.7", | ||
"@types/yargs": "^6.6.0", | ||
"@types/yargs": "^8.0.0", | ||
"ansi-regex": "^3.0.0", | ||
@@ -45,2 +45,3 @@ "common-tags": "^1.3.0", | ||
"node-fetch": "^1.5.3", | ||
"sjcl": "^1.0.6", | ||
"source-map-support": "^0.4.15", | ||
@@ -47,0 +48,0 @@ "yargs": "^8.0.1" |
@@ -36,2 +36,3 @@ import { stripIndent } from 'common-tags'; | ||
let generator; | ||
let resetGenerator; | ||
let compileFromSource; | ||
@@ -41,14 +42,17 @@ let addFragment; | ||
beforeEach(function() { | ||
const context = { | ||
schema: schema, | ||
operations: {}, | ||
fragments: {}, | ||
typesUsed: {} | ||
} | ||
generator = new CodeGenerator(context); | ||
resetGenerator = () => { | ||
const context = { | ||
schema: schema, | ||
operations: {}, | ||
fragments: {}, | ||
typesUsed: {} | ||
} | ||
generator = new CodeGenerator(context); | ||
}; | ||
compileFromSource = (source) => { | ||
compileFromSource = (source, options = { generateOperationIds: false }) => { | ||
const document = parse(source); | ||
const context = compileToIR(schema, document); | ||
let context = compileToIR(schema, document); | ||
options.generateOperationIds && Object.assign(context, { generateOperationIds: true, operationIdsMap: {} }); | ||
generator.context = context; | ||
@@ -61,2 +65,4 @@ return context; | ||
}; | ||
resetGenerator(); | ||
}); | ||
@@ -66,3 +72,3 @@ | ||
test(`should generate a class declaration for a query with variables`, function() { | ||
const { operations } = compileFromSource(` | ||
const { operations, fragments } = compileFromSource(` | ||
query HeroName($episode: Episode) { | ||
@@ -75,3 +81,3 @@ hero(episode: $episode) { | ||
classDeclarationForOperation(generator, operations['HeroName']); | ||
classDeclarationForOperation(generator, operations['HeroName'], Object.values(fragments)); | ||
expect(generator.output).toMatchSnapshot(); | ||
@@ -81,3 +87,3 @@ }); | ||
test(`should generate a class declaration for a query with fragment spreads`, function() { | ||
const { operations } = compileFromSource(` | ||
const { operations, fragments } = compileFromSource(` | ||
query Hero { | ||
@@ -94,3 +100,3 @@ hero { | ||
classDeclarationForOperation(generator, operations['Hero']); | ||
classDeclarationForOperation(generator, operations['Hero'], Object.values(fragments)); | ||
expect(generator.output).toMatchSnapshot(); | ||
@@ -100,3 +106,3 @@ }); | ||
test(`should generate a class declaration for a query with conditional fragment spreads`, function() { | ||
const { operations } = compileFromSource(` | ||
const { operations, fragments } = compileFromSource(` | ||
query Hero { | ||
@@ -113,3 +119,3 @@ hero { | ||
classDeclarationForOperation(generator, operations['Hero']); | ||
classDeclarationForOperation(generator, operations['Hero'], Object.values(fragments)); | ||
expect(generator.output).toMatchSnapshot(); | ||
@@ -119,3 +125,3 @@ }); | ||
test(`should generate a class declaration for a query with a fragment spread nested in an inline fragment`, function() { | ||
const { operations } = compileFromSource(` | ||
const { operations, fragments } = compileFromSource(` | ||
query Hero { | ||
@@ -134,3 +140,3 @@ hero { | ||
classDeclarationForOperation(generator, operations['Hero']); | ||
classDeclarationForOperation(generator, operations['Hero'], Object.values(fragments)); | ||
@@ -141,3 +147,3 @@ expect(generator.output).toMatchSnapshot(); | ||
test(`should generate a class declaration for a mutation with variables`, function() { | ||
const { operations } = compileFromSource(` | ||
const { operations, fragments } = compileFromSource(` | ||
mutation CreateReview($episode: Episode) { | ||
@@ -151,6 +157,145 @@ createReview(episode: $episode, review: { stars: 5, commentary: "Wow!" }) { | ||
classDeclarationForOperation(generator, operations['CreateReview']); | ||
classDeclarationForOperation(generator, operations['CreateReview'], Object.values(fragments)); | ||
expect(generator.output).toMatchSnapshot(); | ||
}); | ||
describe(`when generateOperationIds is specified`, function() { | ||
let compileOptions = { generateOperationIds: true }; | ||
test(`should generate a class declaration with an operationId property`, function() { | ||
const context = compileFromSource(` | ||
query Hero { | ||
hero { | ||
...HeroDetails | ||
} | ||
} | ||
fragment HeroDetails on Character { | ||
name | ||
} | ||
`, compileOptions); | ||
classDeclarationForOperation(generator, context.operations['Hero'], Object.values(context.fragments)); | ||
expect(generator.output).toMatchSnapshot(); | ||
}); | ||
test(`should generate different operation ids for different operations`, function() { | ||
const context1 = compileFromSource(` | ||
query Hero { | ||
hero { | ||
...HeroDetails | ||
} | ||
} | ||
fragment HeroDetails on Character { | ||
name | ||
} | ||
`, compileOptions); | ||
classDeclarationForOperation(generator, context1.operations['Hero'], Object.values(context1.fragments)); | ||
const output1 = generator.output; | ||
resetGenerator(); | ||
const context2 = compileFromSource(` | ||
query Hero { | ||
hero { | ||
...HeroDetails | ||
} | ||
} | ||
fragment HeroDetails on Character { | ||
appearsIn | ||
} | ||
`, compileOptions); | ||
classDeclarationForOperation(generator, context2.operations['Hero'], Object.values(context2.fragments)); | ||
const output2 = generator.output; | ||
expect(output1).not.toBe(output2); | ||
}); | ||
test(`should generate the same operation id regardless of operation formatting/commenting`, function() { | ||
const context1 = compileFromSource(` | ||
query HeroName($episode: Episode) { | ||
hero(episode: $episode) { | ||
name | ||
} | ||
} | ||
`, compileOptions); | ||
classDeclarationForOperation(generator, context1.operations['HeroName'], Object.values(context1.fragments)); | ||
const output1 = generator.output; | ||
resetGenerator(); | ||
const context2 = compileFromSource(` | ||
# Profound comment | ||
query HeroName($episode:Episode) { hero(episode: $episode) { name } } | ||
# Deeply meaningful comment | ||
`, compileOptions); | ||
classDeclarationForOperation(generator, context2.operations['HeroName'], Object.values(context2.fragments)); | ||
const output2 = generator.output; | ||
expect(output1).toBe(output2); | ||
}); | ||
test(`should generate the same operation id regardless of fragment order`, function() { | ||
const context1 = compileFromSource(` | ||
query Hero { | ||
hero { | ||
...HeroName | ||
...HeroAppearsIn | ||
} | ||
} | ||
fragment HeroName on Character { | ||
name | ||
} | ||
fragment HeroAppearsIn on Character { | ||
appearsIn | ||
} | ||
`, compileOptions); | ||
classDeclarationForOperation(generator, context1.operations['Hero'], Object.values(context1.fragments)); | ||
const output1 = generator.output; | ||
resetGenerator(); | ||
const context2 = compileFromSource(` | ||
query Hero { | ||
hero { | ||
...HeroName | ||
...HeroAppearsIn | ||
} | ||
} | ||
fragment HeroAppearsIn on Character { | ||
appearsIn | ||
} | ||
fragment HeroName on Character { | ||
name | ||
} | ||
`, compileOptions); | ||
classDeclarationForOperation(generator, context2.operations['Hero'], Object.values(context2.fragments)); | ||
const output2 = generator.output; | ||
expect(output1).toBe(output2); | ||
}); | ||
test(`should generate appropriate operation id mapping source when there are nested fragment references`, function() { | ||
const source = ` | ||
query Hero { | ||
hero { | ||
...HeroDetails | ||
} | ||
} | ||
fragment HeroName on Character { | ||
name | ||
} | ||
fragment HeroDetails on Character { | ||
...HeroName | ||
appearsIn | ||
} | ||
`; | ||
const context = compileFromSource(source, true); | ||
expect(context.operations['Hero'].sourceWithFragments).toMatchSnapshot(); | ||
}); | ||
}); | ||
}); | ||
@@ -157,0 +302,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
485781
8625
10
+ Addedsjcl@^1.0.6
+ Addedsjcl@1.0.8(transitive)