apollo-codegen
Advanced tools
Comparing version 0.7.1 to 0.7.2
@@ -57,3 +57,4 @@ #!/usr/bin/env node | ||
default: 'schema.json', | ||
normalize: true | ||
normalize: true, | ||
coerce: _path2.default.resolve | ||
}, | ||
@@ -132,12 +133,20 @@ header: { | ||
default: 'schema.json', | ||
normalize: true | ||
normalize: true, | ||
coerce: _path2.default.resolve | ||
}, | ||
output: { | ||
demand: true, | ||
describe: 'Output directory for the generated files', | ||
normalize: true | ||
normalize: true, | ||
coerce: _path2.default.resolve | ||
}, | ||
target: { | ||
demand: false, | ||
describe: 'Code generation target language' | ||
describe: 'Code generation target language', | ||
choices: ['swift', 'json'], | ||
default: 'swift' | ||
}, | ||
"passthrough-custom-scalars": { | ||
demand: false, | ||
describe: "Don't attempt to map custom scalars [temporary option]", | ||
default: false | ||
} | ||
@@ -148,5 +157,4 @@ }, function (argv) { | ||
}); | ||
var schemaPath = _path2.default.resolve(argv.schema); | ||
var outputPath = _path2.default.resolve(argv.output); | ||
(0, _.generate)(inputPaths, schemaPath, outputPath, argv.target); | ||
var options = { passthroughCustomScalars: argv["passthrough-custom-scalars"] }; | ||
(0, _.generate)(inputPaths, argv.schema, argv.output, argv.target, options); | ||
}).fail(function (message, error) { | ||
@@ -153,0 +161,0 @@ handleError(error ? error : new _errors.ToolError(message)); |
@@ -11,2 +11,6 @@ 'use strict'; | ||
var _assign = require('babel-runtime/core-js/object/assign'); | ||
var _assign2 = _interopRequireDefault(_assign); | ||
exports.default = generate; | ||
@@ -30,3 +34,3 @@ | ||
function generate(inputPaths, schemaPath, outputPath, target) { | ||
function generate(inputPaths, schemaPath, outputPath, target, options) { | ||
var schema = (0, _loading.loadSchema)(schemaPath); | ||
@@ -39,6 +43,19 @@ | ||
var context = (0, _compilation.compileToIR)(schema, document); | ||
(0, _assign2.default)(context, options); | ||
var output = target && target.toLowerCase() === 'json' ? generateIR(context) : (0, _swift.generateSource)(context); | ||
var output = void 0; | ||
switch (target) { | ||
case 'json': | ||
output = generateIR(context); | ||
break; | ||
default: | ||
output = (0, _swift.generateSource)(context); | ||
break; | ||
} | ||
_fs2.default.writeFileSync(outputPath, output); | ||
if (outputPath) { | ||
_fs2.default.writeFileSync(outputPath, output); | ||
} else { | ||
console.log(output); | ||
} | ||
} | ||
@@ -45,0 +62,0 @@ |
@@ -120,3 +120,3 @@ 'use strict'; | ||
generator.printNewlineIfNeeded(); | ||
(0, _language.propertyDeclarations)(generator, propertiesFromFields(variables)); | ||
(0, _language.propertyDeclarations)(generator, propertiesFromFields(generator.context, variables)); | ||
generator.printNewlineIfNeeded(); | ||
@@ -141,3 +141,3 @@ initializerDeclarationForVariables(generator, variables); | ||
var type = _ref2.type; | ||
return (0, _printing.join)([name + ': ' + (0, _types.typeNameFromGraphQLType)(type), !(type instanceof _graphql.GraphQLNonNull) && ' = nil']); | ||
return (0, _printing.join)([name + ': ' + (0, _types.typeNameFromGraphQLType)(generator.context, type), !(type instanceof _graphql.GraphQLNonNull) && ' = nil']); | ||
}), ', ')); | ||
@@ -217,3 +217,3 @@ generator.print(')'); | ||
var properties = fields && propertiesFromFields(fields); | ||
var properties = fields && propertiesFromFields(generator.context, fields); | ||
@@ -371,9 +371,9 @@ var fragmentProperties = fragmentSpreads && fragmentSpreads.map(function (fragmentName) { | ||
function propertiesFromFields(fields) { | ||
function propertiesFromFields(context, fields) { | ||
return fields.map(function (field) { | ||
return propertyFromField(field); | ||
return propertyFromField(context, field); | ||
}); | ||
} | ||
function propertyFromField(field) { | ||
function propertyFromField(context, field) { | ||
var fieldName = field.name; | ||
@@ -392,7 +392,7 @@ var fieldType = field.type; | ||
if (namedType instanceof _graphql.GraphQLScalarType || namedType instanceof _graphql.GraphQLEnumType) { | ||
var typeName = (0, _types.typeNameFromGraphQLType)(fieldType); | ||
var typeName = (0, _types.typeNameFromGraphQLType)(context, fieldType); | ||
return (0, _extends3.default)({}, property, { typeName: typeName, isComposite: false }); | ||
} else if ((0, _graphql.isCompositeType)(namedType)) { | ||
var bareTypeName = (0, _changeCase.pascalCase)(_inflected2.default.singularize(propertyName)); | ||
var _typeName = (0, _types.typeNameFromGraphQLType)(fieldType, bareTypeName); | ||
var _typeName = (0, _types.typeNameFromGraphQLType)(context, fieldType, bareTypeName); | ||
return (0, _extends3.default)({}, property, { typeName: _typeName, bareTypeName: bareTypeName, fields: field.fields, isComposite: true, fragmentSpreads: fragmentSpreads, inlineFragments: inlineFragments }); | ||
@@ -399,0 +399,0 @@ } else { |
@@ -26,7 +26,7 @@ 'use strict'; | ||
function typeNameFromGraphQLType(type, bareTypeName) { | ||
var nullable = arguments.length <= 2 || arguments[2] === undefined ? true : arguments[2]; | ||
function typeNameFromGraphQLType(context, type, bareTypeName) { | ||
var nullable = arguments.length <= 3 || arguments[3] === undefined ? true : arguments[3]; | ||
if (type instanceof _graphql.GraphQLNonNull) { | ||
return typeNameFromGraphQLType(type.ofType, bareTypeName, false); | ||
return typeNameFromGraphQLType(context, type.ofType, bareTypeName, false); | ||
} | ||
@@ -36,5 +36,5 @@ | ||
if (type instanceof _graphql.GraphQLList) { | ||
typeName = '[' + typeNameFromGraphQLType(type.ofType, bareTypeName, true) + ']'; | ||
typeName = '[' + typeNameFromGraphQLType(context, type.ofType, bareTypeName, true) + ']'; | ||
} else if (type instanceof _graphql.GraphQLScalarType) { | ||
typeName = builtInScalarMap[type.name] || _graphql.GraphQLString; | ||
typeName = builtInScalarMap[type.name] || (context.passthroughCustomScalars ? type.name : _graphql.GraphQLString); | ||
} else { | ||
@@ -41,0 +41,0 @@ typeName = bareTypeName || type.name; |
{ | ||
"name": "apollo-codegen", | ||
"version": "0.7.1", | ||
"version": "0.7.2", | ||
"description": "Generate client code based on a GraphQL schema and query documents", | ||
@@ -5,0 +5,0 @@ "main": "./lib/index.js", |
@@ -1,11 +0,9 @@ | ||
# Apollo client code generator | ||
# Apollo GraphQL code generator | ||
This is a tool to generate client code based on a GraphQL schema and query documents. | ||
[![GitHub license](https://img.shields.io/badge/license-MIT-lightgrey.svg?maxAge=2592000)](https://raw.githubusercontent.com/apollostack/apollo-ios/master/LICENSE) [![npm](https://img.shields.io/npm/v/apollo-codegen.svg?maxAge=2592000)](https://www.npmjs.com/package/apollo-codegen) [![Get on Slack](https://img.shields.io/badge/slack-join-orange.svg)](http://www.apollostack.com/#slack) | ||
It currently only generates Swift code, and only for a subset of GraphQL queries. Most importantly, fragments are only supported if their type condition matches their parent type exactly. See [Apollo iOS](https://github.com/apollostack/apollo-ios) for details on the proposed mapping from GraphQL results to Swift types, as well as runtime support for performing queries. | ||
This is a tool to generate API code based on a GraphQL schema and query documents. | ||
## Getting Started | ||
It currently only generates Swift code, but we hope to add support for other targets in the future. See [Apollo iOS](https://github.com/apollostack/apollo-ios) for details on the mapping from GraphQL results to Swift types, as well as runtime support for executing queries and mutations. | ||
[Apollo iOS Quickstart](https://github.com/apollostack/apollo-ios-quickstart) is a sample Xcode project that makes it easy to get started with a generated API for your own schema and queries. | ||
## Usage | ||
@@ -16,3 +14,3 @@ | ||
```sh | ||
npm install apollo-codegen -g | ||
npm install -g apollo-codegen | ||
``` | ||
@@ -23,3 +21,3 @@ | ||
```sh | ||
apollo-codegen download-schema http://localhost:8080/graphql --output GitHuntAPI/Definitions/schema.json | ||
apollo-codegen download-schema http://localhost:8080/graphql --output schema.json | ||
``` | ||
@@ -32,3 +30,3 @@ | ||
```sh | ||
apollo-codegen generate GitHuntAPI/Definitions/**/*.graphql --schema GitHuntAPI/Definitions/schema.json --output GitHuntAPI/GitHuntAPI.swift | ||
apollo-codegen generate **/*.graphql --schema schema.json --output API.swift | ||
``` |
@@ -26,44 +26,48 @@ import { expect } from 'chai'; | ||
it('should return String? for GraphQLString', function() { | ||
expect(typeNameFromGraphQLType(GraphQLString)).to.equal('String?'); | ||
expect(typeNameFromGraphQLType({}, GraphQLString)).to.equal('String?'); | ||
}); | ||
it('should return String for GraphQLNonNull(GraphQLString)', function() { | ||
expect(typeNameFromGraphQLType(new GraphQLNonNull(GraphQLString))).to.equal('String'); | ||
expect(typeNameFromGraphQLType({}, new GraphQLNonNull(GraphQLString))).to.equal('String'); | ||
}); | ||
it('should return [String?]? for GraphQLList(GraphQLString)', function() { | ||
expect(typeNameFromGraphQLType(new GraphQLList(GraphQLString))).to.equal('[String?]?'); | ||
expect(typeNameFromGraphQLType({}, new GraphQLList(GraphQLString))).to.equal('[String?]?'); | ||
}); | ||
it('should return [String?] for GraphQLNonNull(GraphQLList(GraphQLString))', function() { | ||
expect(typeNameFromGraphQLType(new GraphQLNonNull(new GraphQLList(GraphQLString)))).to.equal('[String?]'); | ||
expect(typeNameFromGraphQLType({}, new GraphQLNonNull(new GraphQLList(GraphQLString)))).to.equal('[String?]'); | ||
}); | ||
it('should return [String]? for GraphQLList(GraphQLNonNull(GraphQLString))', function() { | ||
expect(typeNameFromGraphQLType(new GraphQLList(new GraphQLNonNull(GraphQLString)))).to.equal('[String]?'); | ||
expect(typeNameFromGraphQLType({}, new GraphQLList(new GraphQLNonNull(GraphQLString)))).to.equal('[String]?'); | ||
}); | ||
it('should return [String] for GraphQLNonNull(GraphQLList(GraphQLNonNull(GraphQLString)))', function() { | ||
expect(typeNameFromGraphQLType(new GraphQLNonNull(new GraphQLList(new GraphQLNonNull(GraphQLString))))).to.equal('[String]'); | ||
expect(typeNameFromGraphQLType({}, new GraphQLNonNull(new GraphQLList(new GraphQLNonNull(GraphQLString))))).to.equal('[String]'); | ||
}); | ||
it('should return Int? for GraphQLInt', function() { | ||
expect(typeNameFromGraphQLType(GraphQLInt)).to.equal('Int?'); | ||
expect(typeNameFromGraphQLType({}, GraphQLInt)).to.equal('Int?'); | ||
}); | ||
it('should return Float? for GraphQLFloat', function() { | ||
expect(typeNameFromGraphQLType(GraphQLFloat)).to.equal('Float?'); | ||
expect(typeNameFromGraphQLType({}, GraphQLFloat)).to.equal('Float?'); | ||
}); | ||
it('should return Bool? for GraphQLBoolean', function() { | ||
expect(typeNameFromGraphQLType(GraphQLBoolean)).to.equal('Bool?'); | ||
expect(typeNameFromGraphQLType({}, GraphQLBoolean)).to.equal('Bool?'); | ||
}); | ||
it('should return GraphQLID? for GraphQLID', function() { | ||
expect(typeNameFromGraphQLType(GraphQLID)).to.equal('GraphQLID?'); | ||
expect(typeNameFromGraphQLType({}, GraphQLID)).to.equal('GraphQLID?'); | ||
}); | ||
it('should return String? for a custom scalar type', function() { | ||
expect(typeNameFromGraphQLType(new GraphQLScalarType({ name: 'CustomScalarType', serialize: String }))).to.equal('String?'); | ||
expect(typeNameFromGraphQLType({}, new GraphQLScalarType({ name: 'CustomScalarType', serialize: String }))).to.equal('String?'); | ||
}); | ||
it('should return a passed through custom scalar type with the passthroughCustomScalars option', function() { | ||
expect(typeNameFromGraphQLType({ passthroughCustomScalars: true }, new GraphQLScalarType({ name: 'CustomScalarType', serialize: String }))).to.equal('CustomScalarType?'); | ||
}); | ||
}); | ||
@@ -70,0 +74,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
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
255785
49
4930
30