Skip to content

Commit

Permalink
Merge pull request #6 from bw-company/creasty/remove_union
Browse files Browse the repository at this point in the history
Abolish union type completely
  • Loading branch information
creasty authored Sep 8, 2020
2 parents dc8c9f3 + 801ffb9 commit c9d516a
Show file tree
Hide file tree
Showing 10 changed files with 125 additions and 1,210 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
node-version: '12.x'
registry-url: 'https://npm.pkg.github.com'
- run: yarn
- run: yarn pull:wkt
- run: yarn pull:google-proto
- run: yarn test
- run: yarn build
- run: yarn publish
Expand Down
10 changes: 3 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@bw-company/proto2graphql",
"version": "0.2.3-alpha",
"version": "0.2.4-alpha",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"description": "Converts schema definitions in Protocol Buffer to GraphQL",
Expand All @@ -12,9 +12,8 @@
"scripts": {
"clean": "rm -rf ./protos",
"build": "tsc",
"test": "nyc mocha -r ts-node/register **/test/*.spec.ts",
"coverage": "nyc report --reporter=text-lcov | coveralls",
"pull:wkt": "scripts/pull-wkt.sh"
"test": "mocha -r ts-node/register **/test/*.spec.ts",
"pull:google-proto": "scripts/pull-google-proto"
},
"dependencies": {
"graphql": "^14.5.8",
Expand All @@ -25,10 +24,7 @@
"@types/graphql": "^14.5.0",
"@types/mocha": "^5.2.7",
"chai": "^4.2.0",
"coveralls": "^3.0.9",
"mocha": "^6.2.2",
"mocha-lcov-reporter": "^1.3.0",
"nyc": "^15.0.0",
"ts-node": "^8.5.4",
"typescript": "^3.7.4"
}
Expand Down
42 changes: 42 additions & 0 deletions scripts/pull-google-proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#!/usr/bin/env bash

set -eu
set -o pipefail

OUTPUT_DIR="./tmp/protos-include"
CACHE_DIR="./tmp/protos-include-cache"

PROTOBUF_VERSION=3.13.0
GOOGLEAPIS_VERSION=a8c73212a73d460b1edcd0830c4c3e31de33bdc5

PROTOBUF_ZIP=https://github.com/protocolbuffers/protobuf/releases/download/v${PROTOBUF_VERSION}/protobuf-js-${PROTOBUF_VERSION}.zip
GOOGLEAPIS_ZIP=https://github.com/googleapis/googleapis/archive/${GOOGLEAPIS_VERSION}.zip

fetch() {
local url="$1"
local name="$2"

local zip_path="$CACHE_DIR/$name.zip"
local unzip_path="$CACHE_DIR/$name"

[ -f "$zip_path" ] || curl -L "$url" -o "$zip_path"
[ -d "$unzip_path" ] || unzip "$zip_path" "*.proto" -d "$CACHE_DIR"

for pair in "${@:3}"; do
local src_dir="$unzip_path/${pair%%:*}"
local dst_dir="$OUTPUT_DIR/${pair#*:}"

[ -d "$dst_dir" ] && rm -rf "$dst_dir"
mkdir -p "$dst_dir"

rsync -av "$src_dir/" "$dst_dir/"
done
}

[ -d "$CACHE_DIR" ] || mkdir -p "$CACHE_DIR"

fetch "$PROTOBUF_ZIP" "protobuf-$PROTOBUF_VERSION" \
"src/google/protobuf:google/protobuf"
fetch "$GOOGLEAPIS_ZIP" "googleapis-$GOOGLEAPIS_VERSION" \
"google/api:google/api" \
"google/type:google/type"
40 changes: 0 additions & 40 deletions scripts/pull-wkt.sh

This file was deleted.

47 changes: 24 additions & 23 deletions src/visitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,30 +179,31 @@ function createUnionType(
oneOf: protobuf.OneOf,
context: Context
): GraphQLUnionType | GraphQLObjectType {
// FIXME: This doesn't work. Needs deferred evaluation.
// A union type can only include object types
const compatible = oneOf.fieldsArray
.map((field) => createOutputFieldType(field, context, true))
.every((type) => !type || isObjectType(type));

if (compatible) {
const unionType = new GraphQLUnionType({
name: context.getFullTypeName(oneOf),
types: () => {
return oneOf.fieldsArray
.map((field) => createOutputFieldType(field, context, true))
.filter(Boolean) as GraphQLObjectType[];
},
});
context.setType(unionType);
return unionType;
} else {
const objectType = new GraphQLObjectType({
name: context.getFullTypeName(oneOf),
fields: () => createOutputFields(oneOf.fieldsArray, false, context),
});
context.setType(objectType);
return objectType;
}
// const compatible = oneOf.fieldsArray
// .map((field) => createOutputFieldType(field, context, true))
// .every((type) => !type || isObjectType(type));
//
// if (compatible) {
// const unionType = new GraphQLUnionType({
// name: context.getFullTypeName(oneOf),
// types: () => {
// return oneOf.fieldsArray
// .map((field) => createOutputFieldType(field, context, true))
// .filter(Boolean) as GraphQLObjectType[];
// },
// });
// context.setType(unionType);
// return unionType;
// }

const objectType = new GraphQLObjectType({
name: context.getFullTypeName(oneOf),
fields: () => createOutputFields(oneOf.fieldsArray, false, context),
});
context.setType(objectType);
return objectType;
}

function createInputUnionType(
Expand Down
5 changes: 4 additions & 1 deletion test/field_behavior/output.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ input TypeAInput {
fieldA: TypeA_TypeBInput
}

union TypeA_either = google_protobuf_Int32Value | google_protobuf_StringValue
type TypeA_either {
int32Value: google_protobuf_Int32Value
stringValue: google_protobuf_StringValue
}

input TypeA_eitherInput {
int32Value: google_protobuf_Int32ValueInput
Expand Down
9 changes: 9 additions & 0 deletions test/oneof_fields/external.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
syntax = "proto3";

enum ExternalEnum {
UNSPECIFIED = 0;
}

message ExternalMessage {
string foo = 1;
}
27 changes: 17 additions & 10 deletions test/oneof_fields/input.proto
Original file line number Diff line number Diff line change
@@ -1,28 +1,35 @@
syntax = "proto3";

import "./external.proto";

message UserAuth {
oneof provider {
GoogleProvider google = 1;
FacebookProvider facebook = 2;
}

message GoogleProvider {
int32 gid = 1;
}

message FacebookProvider {
string fbid = 1;
}

oneof provider {
GoogleProvider google = 1;
FacebookProvider facebook = 2;
}
}

message Redirect {
enum NamedDestination {
HOME = 0;
ACCOUNT = 1;
}

oneof destination {
NamedDestination named_destination = 1;
string generic = 2;
}

oneof destination_2 {
ExternalEnum external_enum = 3;
ExternalMessage external_message = 4;
}

enum NamedDestination {
HOME = 0;
ACCOUNT = 1;
}
}
19 changes: 18 additions & 1 deletion test/oneof_fields/output.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ type UserAuth {
provider: UserAuth_provider
}

union UserAuth_provider = UserAuth_GoogleProvider | UserAuth_FacebookProvider
type UserAuth_provider {
google: UserAuth_GoogleProvider
facebook: UserAuth_FacebookProvider
}

type UserAuth_GoogleProvider {
gid: Int!
Expand All @@ -14,14 +17,28 @@ type UserAuth_FacebookProvider {

type Redirect {
destination: Redirect_destination
destination_2: Redirect_destination_2
}

type Redirect_destination {
namedDestination: Redirect_NamedDestination
generic: String
}

type Redirect_destination_2 {
externalEnum: ExternalEnum
externalMessage: ExternalMessage
}

enum Redirect_NamedDestination {
HOME
ACCOUNT
}

enum ExternalEnum {
UNSPECIFIED
}

type ExternalMessage {
foo: String!
}
Loading

0 comments on commit c9d516a

Please sign in to comment.