Skip to content

Commit

Permalink
feat(sofa): add rest endpoints as well
Browse files Browse the repository at this point in the history
  • Loading branch information
maapteh committed Jan 14, 2020
1 parent e59c8fb commit af50892
Show file tree
Hide file tree
Showing 11 changed files with 488 additions and 47 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,15 @@ GRAPHQL_ENDPOINT=endpoint-your-graphql-server-will-run
- [GraphQL HQ](https://blog.apollographql.com/)


## NON-BELIEVERS
There are always teams resistent to pickup "new" technologies. If they want they are still able to consume us as rest endpoints with the same codebase behind it.

For example our application also gives the following endpoint:
http://localhost:4000/api/get-product/9200000111963040

See [open-api](./packages/server/swagger.yml) its auto generated with help of [SOFA](https://github.com/Urigo/SOFA)


## VSC plugins
- [vscode-apollo](https://marketplace.visualstudio.com/items?itemName=apollographql.vscode-apollo) for autocomplete in app
- [eslint](https://marketplace.visualstudio.com/items?itemName=dbaeumer.vscode-eslint) including apollo linting
Expand Down
4 changes: 2 additions & 2 deletions packages/app/src/graphql/_generated-hooks.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ export type ProductOfferDataOffer = {
availabilityDescription: Scalars['String'],
comment: Scalars['String'],
seller: ProductSeller,
bestOffer: Scalars['Boolean'],
releaseDate: Scalars['String'],
bestOffer?: Maybe<Scalars['Boolean']>,
releaseDate?: Maybe<Scalars['String']>,
};

export type ProductParentCategory = {
Expand Down
4 changes: 2 additions & 2 deletions packages/app/src/graphql/_generated-schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ type ProductOfferDataOffer {
availabilityDescription: String!
comment: String!
seller: ProductSeller!
bestOffer: Boolean!
releaseDate: String!
bestOffer: Boolean
releaseDate: String
}

type ProductParentCategory {
Expand Down
4 changes: 2 additions & 2 deletions packages/app/src/graphql/_generated-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ export type ProductOfferDataOffer = {
availabilityDescription: Scalars['String'],
comment: Scalars['String'],
seller: ProductSeller,
bestOffer: Scalars['Boolean'],
releaseDate: Scalars['String'],
bestOffer?: Maybe<Scalars['Boolean']>,
releaseDate?: Maybe<Scalars['String']>,
};

export type ProductParentCategory = {
Expand Down
1 change: 1 addition & 0 deletions packages/server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
"graphql-voyager": "1.0.0-rc.28",
"node-fetch": "2.6.0",
"reflect-metadata": "0.1.13",
"sofa-api": "0.6.0",
"ts-node": "8.5.4",
"ts-node-dev": "1.0.0-pre.44",
"typescript": "3.7.3"
Expand Down
8 changes: 4 additions & 4 deletions packages/server/src/_graphql.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ export type ProductOfferDataOffer = {
availabilityDescription: Scalars['String'],
comment: Scalars['String'],
seller: ProductSeller,
bestOffer: Scalars['Boolean'],
releaseDate: Scalars['String'],
bestOffer?: Maybe<Scalars['Boolean']>,
releaseDate?: Maybe<Scalars['String']>,
};

export type ProductParentCategory = {
Expand Down Expand Up @@ -327,8 +327,8 @@ export type ProductOfferDataOfferResolvers<ContextType = any, ParentType extends
availabilityDescription?: Resolver<ResolversTypes['String'], ParentType, ContextType>,
comment?: Resolver<ResolversTypes['String'], ParentType, ContextType>,
seller?: Resolver<ResolversTypes['ProductSeller'], ParentType, ContextType>,
bestOffer?: Resolver<ResolversTypes['Boolean'], ParentType, ContextType>,
releaseDate?: Resolver<ResolversTypes['String'], ParentType, ContextType>,
bestOffer?: Resolver<Maybe<ResolversTypes['Boolean']>, ParentType, ContextType>,
releaseDate?: Resolver<Maybe<ResolversTypes['String']>, ParentType, ContextType>,
};

export type ProductParentCategoryResolvers<ContextType = any, ParentType extends ResolversParentTypes['ProductParentCategory'] = ResolversParentTypes['ProductParentCategory']> = {
Expand Down
4 changes: 2 additions & 2 deletions packages/server/src/_schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ type ProductOfferDataOffer {
availabilityDescription: String!
comment: String!
seller: ProductSeller!
bestOffer: Boolean!
releaseDate: String!
bestOffer: Boolean
releaseDate: String
}

type ProductParentCategory {
Expand Down
4 changes: 2 additions & 2 deletions packages/server/src/modules/product/schema/product.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ type ProductOfferDataOffer {
availabilityDescription: String!
comment: String!
seller: ProductSeller!
bestOffer: Boolean!
releaseDate: String!
bestOffer: Boolean
releaseDate: String
}

type ProductSeller {
Expand Down
24 changes: 24 additions & 0 deletions packages/server/src/server.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import express from 'express';
import { useSofa, OpenAPI } from 'sofa-api';
import { GraphQLModule } from '@graphql-modules/core';
import { ApolloServer } from 'apollo-server-express';
import { express as voyagerMiddleware } from 'graphql-voyager/middleware';
Expand Down Expand Up @@ -63,6 +64,29 @@ export async function bootstrap(appModule: GraphQLModule) {

const app = express();

// FOR NON-BELIEVERS provide old rest endpoints
const openApi = OpenAPI({
schema,
info: {
title: 'Example API',
version: '3.0.0',
},
});

app.use(
'/api',
useSofa({
schema,
onRoute(info) {
openApi.addRoute(info, {
basePath: '/api',
});
},
})
);

openApi.save('./swagger.yml');

// ADD GZIP
app.use(compression({ filter: shouldCompress }));
app.use(NO_CACHE);
Expand Down
Loading

0 comments on commit af50892

Please sign in to comment.