Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
- `apollo-codegen-core`
- <First `apollo-codegen-core` related entry goes here>
- `apollo-codegen-flow`
- <First `apollo-codegen-flow` related entry goes here>
- Renamed `useFlowReadOnlyTypes` option to `useReadOnlyTypes` [#1205](https://github.com/apollographql/apollo-tooling/pull/1205)
- `apollo-codegen-scala`
- <First `apollo-codegen-scala` related entry goes here>
- `apollo-codegen-swift`
- <First `apollo-codegen-swift` related entry goes here>
- `apollo-codegen-typescript`
- <First `apollo-codegen-typescript` related entry goes here>
- Added `useReadOnlyTypes` option to use readonly types [#1205](https://github.com/apollographql/apollo-tooling/pull/1205)
- `apollo-env`
- <First `apollo-env` related entry goes here>
- `apollo-graphql`
Expand Down
1 change: 1 addition & 0 deletions packages/apollo-codegen-core/src/compiler/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export interface CompilerOptions {
namespace?: string;
generateOperationIds?: boolean;
operationIdsPath?: string;
useReadOnlyTypes?: boolean;
}

export interface CompilerContext {
Expand Down
4 changes: 2 additions & 2 deletions packages/apollo-codegen-flow/src/__tests__/codeGeneration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ function compile(
options: FlowCompilerOptions = {
mergeInFieldsFromFragmentSpreads: true,
useFlowExactObjects: false,
useFlowReadOnlyTypes: false,
useReadOnlyTypes: false,
addTypename: true
}
): CompilerContext {
Expand Down Expand Up @@ -286,7 +286,7 @@ describe("Flow codeGeneration", () => {
`,
{
mergeInFieldsFromFragmentSpreads: true,
useFlowReadOnlyTypes: true,
useReadOnlyTypes: true,
useFlowExactObjects: true,
addTypename: true
}
Expand Down
6 changes: 3 additions & 3 deletions packages/apollo-codegen-flow/src/__tests__/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { createTypeAnnotationFromGraphQLTypeFunction } from "../helpers";
const typeAnnotationFromGraphQLType = createTypeAnnotationFromGraphQLTypeFunction(
{
passthroughCustomScalars: false,
useFlowReadOnlyTypes: false
useReadOnlyTypes: false
}
);

Expand Down Expand Up @@ -438,7 +438,7 @@ describe("passthrough custom scalars", () => {
beforeAll(() => {
getTypeAnnotation = createTypeAnnotationFromGraphQLTypeFunction({
passthroughCustomScalars: true,
useFlowReadOnlyTypes: false
useReadOnlyTypes: false
});
});

Expand All @@ -463,7 +463,7 @@ describe("passthrough custom scalars with custom scalar prefix", () => {
getTypeAnnotation = createTypeAnnotationFromGraphQLTypeFunction({
passthroughCustomScalars: true,
customScalarsPrefix: "Foo$",
useFlowReadOnlyTypes: false
useReadOnlyTypes: false
});
});

Expand Down
8 changes: 2 additions & 6 deletions packages/apollo-codegen-flow/src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,10 @@ const builtInScalarMap = {
[GraphQLID.name]: t.stringTypeAnnotation()
};

export interface FlowCompilerOptions extends CompilerOptions {
Comment thread
mike-marcacci marked this conversation as resolved.
useFlowReadOnlyTypes: boolean;
}

export function createTypeAnnotationFromGraphQLTypeFunction(
compilerOptions: FlowCompilerOptions
compilerOptions: CompilerOptions
): Function {
const arrayType = compilerOptions.useFlowReadOnlyTypes
const arrayType = compilerOptions.useReadOnlyTypes
? "$ReadOnlyArray"
: "Array";

Expand Down
3 changes: 1 addition & 2 deletions packages/apollo-codegen-flow/src/language.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ export type ObjectProperty = {

export interface FlowCompilerOptions extends CompilerOptions {
useFlowExactObjects: boolean;
useFlowReadOnlyTypes: boolean;
}

export default class FlowGenerator {
Expand Down Expand Up @@ -102,7 +101,7 @@ export default class FlowGenerator {
objectTypeProperty.optional =
keyInheritsNullability &&
annotation.type === "NullableTypeAnnotation";
if (this.options.useFlowReadOnlyTypes) {
if (this.options.useReadOnlyTypes) {
objectTypeProperty.variance = { kind: "plus" };
}

Expand Down
34 changes: 31 additions & 3 deletions packages/apollo-codegen-typescript/src/__tests__/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ import * as t from "@babel/types";
import { createTypeFromGraphQLTypeFunction } from "../helpers";

const typeFromGraphQLType = createTypeFromGraphQLTypeFunction({
passthroughCustomScalars: false
passthroughCustomScalars: false,
useReadOnlyTypes: false
});

function nullableType(type: t.TSType) {
Expand Down Expand Up @@ -288,7 +289,8 @@ describe("passthrough custom scalars", () => {

beforeAll(() => {
getTypeAnnotation = createTypeFromGraphQLTypeFunction({
passthroughCustomScalars: true
passthroughCustomScalars: true,
useReadOnlyTypes: false
});
});

Expand All @@ -312,7 +314,8 @@ describe("passthrough custom scalars with custom scalar prefix", () => {
beforeAll(() => {
getTypeAnnotation = createTypeFromGraphQLTypeFunction({
passthroughCustomScalars: true,
customScalarsPrefix: "Foo$"
customScalarsPrefix: "Foo$",
useReadOnlyTypes: false
});
});

Expand All @@ -329,3 +332,28 @@ describe("passthrough custom scalars with custom scalar prefix", () => {
);
});
});

describe("readonly arrays", () => {
let getTypeAnnotation: Function;

beforeAll(() => {
getTypeAnnotation = createTypeFromGraphQLTypeFunction({
useReadOnlyTypes: true
});
});

test("Readonly array", () => {
const OddType = new GraphQLList(GraphQLString);

expect(getTypeAnnotation(OddType)).toMatchObject(
nullableType(
t.TSTypeReference(
t.identifier("ReadonlyArray"),
t.TSTypeParameterInstantiation([
t.TSParenthesizedType(nullableType(t.TSStringKeyword()))
])
)
)
);
});
});
10 changes: 9 additions & 1 deletion packages/apollo-codegen-typescript/src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,21 @@ const builtInScalarMap = {
export function createTypeFromGraphQLTypeFunction(
compilerOptions: CompilerOptions
): (graphQLType: GraphQLType, typeName?: string) => t.TSType {
const ArrayType = compilerOptions.useReadOnlyTypes
? (e: t.TSType) =>
t.TSTypeReference(
t.identifier("ReadonlyArray"),
t.TSTypeParameterInstantiation([e])
)
: (e: t.TSType) => t.TSArrayType(e);

function nonNullableTypeFromGraphQLType(
graphQLType: GraphQLType,
typeName?: string
): t.TSType {
if (isListType(graphQLType)) {
const elementType = typeFromGraphQLType(graphQLType.ofType, typeName);
return t.TSArrayType(
return ArrayType(
t.isTSUnionType(elementType)
? t.TSParenthesizedType(elementType)
: elementType
Expand Down
6 changes: 5 additions & 1 deletion packages/apollo-codegen-typescript/src/language.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export type ObjectProperty = {
};

export interface TypescriptCompilerOptions extends CompilerOptions {
// Leaving this open for Typescript only compiler options
useReadOnlyTypes: boolean;
Comment thread
mike-marcacci marked this conversation as resolved.
Outdated
}

export default class TypescriptGenerator {
Expand Down Expand Up @@ -106,6 +106,10 @@ export default class TypescriptGenerator {
propertySignatureType.optional =
keyInheritsNullability && this.isNullableType(type);

if (this.options.useReadOnlyTypes) {
propertySignatureType.readonly = true;
}

if (description) {
propertySignatureType.leadingComments = [
{
Expand Down
32 changes: 18 additions & 14 deletions packages/apollo/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,19 @@
Apollo CLI brings together your GraphQL clients and servers with tools for validating your schema, linting your operations for compatibility with your server, and generating static types for improved client-side type safety.

<!-- toc -->
* [Apollo CLI](#apollo-cli)
Comment thread
mike-marcacci marked this conversation as resolved.
* [Usage](#usage)
* [Commands](#commands)
* [Configuration](#configuration)
* [Code Generation](#code-generation)
* [Contributing](#contributing)
<!-- tocstop -->

- [Apollo CLI](#apollo-cli)
- [Usage](#usage)
- [Commands](#commands)
- [Configuration](#configuration)
- [Code Generation](#code-generation)
- [Contributing](#contributing)
<!-- tocstop -->

# Usage

<!-- usage -->

```sh-session
$ npm install -g apollo
$ apollo COMMAND
Expand All @@ -27,6 +29,7 @@ USAGE
$ apollo COMMAND
...
```

<!-- usagestop -->

# Commands
Expand Down Expand Up @@ -100,11 +103,11 @@ ARGUMENTS
OUTPUT
Directory to which generated files will be written.
- For TypeScript/Flow generators, this specifies a directory relative to each source file by default.
- For TypeScript/Flow generators with the "outputFlat" flag is set, and for the Swift generator, this specifies a
- For TypeScript/Flow generators with the "outputFlat" flag is set, and for the Swift generator, this specifies a
file or directory (absolute or relative to the current working directory) to which:
- a file will be written for each query (if "output" is a directory)
- all generated types will be written
- For all other types, this defines a file (absolute or relative to the current working directory) to which all
- For all other types, this defines a file (absolute or relative to the current working directory) to which all
generated types are written.

OPTIONS
Expand Down Expand Up @@ -170,7 +173,7 @@ OPTIONS

--useFlowExactObjects Use Flow exact objects for generated types [flow only]

--useFlowReadOnlyTypes Use Flow read only types for generated types [flow only]
--useReadOnlyTypes Use Flow read only types for generated types [flow | typescript]

--watch Watch for file changes and reload codegen

Expand Down Expand Up @@ -361,15 +364,15 @@ DESCRIPTION

Installation of a user-installed plugin will override a core plugin.

e.g. If you have a core plugin that has a 'hello' command, installing a user-installed plugin with a 'hello' command
will override the core plugin implementation. This is useful if a user needs to update core plugin functionality in
e.g. If you have a core plugin that has a 'hello' command, installing a user-installed plugin with a 'hello' command
will override the core plugin implementation. This is useful if a user needs to update core plugin functionality in
the CLI without the need to patch and update the whole CLI.

ALIASES
$ apollo plugins:add

EXAMPLES
$ apollo plugins:install myplugin
$ apollo plugins:install myplugin
$ apollo plugins:install https://github.com/someuser/someplugin
$ apollo plugins:install someuser/someplugin
```
Expand All @@ -394,7 +397,7 @@ OPTIONS
DESCRIPTION
Installation of a linked plugin will override a user-installed or core plugin.

e.g. If you have a user-installed or core plugin that has a 'hello' command, installing a linked plugin with a 'hello'
e.g. If you have a user-installed or core plugin that has a 'hello' command, installing a linked plugin with a 'hello'
command will override the user-installed or core plugin implementation. This is useful for development work.

EXAMPLE
Expand Down Expand Up @@ -581,6 +584,7 @@ ALIASES
```

_See code: [src/commands/service/push.ts](https://github.com/apollographql/apollo-tooling/blob/master/packages/apollo/src/commands/service/push.ts)_

<!-- commandsstop -->

# Configuration
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ describe("client:codegen", () => {
"--config=my.config.js",
"--target=flow",
"--outputFlat",
"--useFlowReadOnlyTypes",
"--useReadOnlyTypes",
"__tmp__API.js"
])
.it("writes read-only Flow types when the flag is set", () => {
Expand Down
9 changes: 5 additions & 4 deletions packages/apollo/src/commands/client/codegen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,12 @@ export default class Generate extends ClientCommand {
useFlowExactObjects: flags.boolean({
description: "Use Flow exact objects for generated types [flow only]"
}),
useFlowReadOnlyTypes: flags.boolean({
Comment thread
mike-marcacci marked this conversation as resolved.
description: "Use Flow read only types for generated types [flow only]"
}),

// flow / TS
useReadOnlyTypes: flags.boolean({
description: "Use read only types for generated types [flow | typescript]"
}),

outputFlat: flags.boolean({
description:
'By default, TypeScript/Flow will put each generated file in a directory next to its source file using the value of the "output" as the directory name. Set "outputFlat" to put all generated files in the directory relative to the current working directory defined by "output".'
Expand Down Expand Up @@ -192,7 +193,7 @@ export default class Generate extends ClientCommand {
mergeInFieldsFromFragmentSpreads:
flags.mergeInFieldsFromFragmentSpreads,
useFlowExactObjects: flags.useFlowExactObjects,
useFlowReadOnlyTypes: flags.useFlowReadOnlyTypes,
useReadOnlyTypes: flags.useReadOnlyTypes,
Comment thread
mike-marcacci marked this conversation as resolved.
Outdated
globalTypesFile: flags.globalTypesFile
}
);
Expand Down