Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
oncampus
Patterns and Frameworks
Commits
b4c805ea
Commit
b4c805ea
authored
Jun 08, 2021
by
Jens Ehlers
Browse files
Added a small GraphQL example to REST-API (AccountResource)
parent
8bc84195
Pipeline
#46448
passed with stage
in 1 minute and 2 seconds
Changes
4
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
remote/rest-server/pom.xml
View file @
b4c805ea
...
...
@@ -80,6 +80,11 @@
<version>
5.3.2
</version>
<scope>
test
</scope>
</dependency>
<dependency>
<groupId>
com.graphql-java
</groupId>
<artifactId>
graphql-java
</artifactId>
<version>
16.2
</version>
</dependency>
</dependencies>
<build>
...
...
remote/rest-server/src/main/java/resources/AccountResourceGraphQL.java
0 → 100644
View file @
b4c805ea
package
resources
;
import
graphql.ExecutionResult
;
import
graphql.GraphQL
;
import
graphql.schema.DataFetcher
;
import
graphql.schema.GraphQLSchema
;
import
graphql.schema.idl.RuntimeWiring
;
import
graphql.schema.idl.SchemaGenerator
;
import
graphql.schema.idl.SchemaParser
;
import
graphql.schema.idl.TypeDefinitionRegistry
;
import
graphql.schema.idl.TypeRuntimeWiring
;
import
model.Account
;
import
javax.ws.rs.GET
;
import
javax.ws.rs.Path
;
import
javax.ws.rs.core.Response
;
import
java.io.IOException
;
import
java.net.URISyntaxException
;
import
java.nio.file.Files
;
import
java.nio.file.Paths
;
import
java.util.Collection
;
import
java.util.List
;
@Path
(
"/accounts_graphql"
)
public
class
AccountResourceGraphQL
{
static
GraphQL
graphQL
;
static
GraphQL
getGraphQL
()
{
if
(
graphQL
!=
null
)
return
graphQL
;
try
{
String
sdl
=
Files
.
readString
(
Paths
.
get
(
ClassLoader
.
getSystemClassLoader
().
getResource
(
"accountSchema.graphqls"
).
toURI
()));
TypeDefinitionRegistry
typeRegistry
=
new
SchemaParser
().
parse
(
sdl
);
DataFetcher
<
Collection
<
Account
>>
accountsDataFetcher
=
(
env
)
->
{
Integer
id
=
env
.
getArgument
(
"id"
);
return
id
==
null
?
AccountResource
.
accounts
.
values
()
:
List
.
of
(
AccountResource
.
accounts
.
get
(
id
));
};
RuntimeWiring
runtimeWiring
=
RuntimeWiring
.
newRuntimeWiring
()
.
type
(
TypeRuntimeWiring
.
newTypeWiring
(
"Query"
).
dataFetcher
(
"accounts"
,
accountsDataFetcher
))
.
build
();
GraphQLSchema
schema
=
new
SchemaGenerator
().
makeExecutableSchema
(
typeRegistry
,
runtimeWiring
);
graphQL
=
GraphQL
.
newGraphQL
(
schema
).
build
();
}
catch
(
URISyntaxException
|
IOException
e
)
{
e
.
printStackTrace
();
}
return
graphQL
;
}
@GET
public
Response
getAccountsByGraphQLQuery
(
String
query
)
{
ExecutionResult
res
=
getGraphQL
().
execute
(
query
);
if
(
res
.
getErrors
().
size
()
>
0
)
{
String
msg
=
res
.
getErrors
().
get
(
0
).
getMessage
();
return
Response
.
status
(
400
).
entity
(
msg
).
build
();
}
String
result
=
res
.
getData
().
toString
();
return
Response
.
ok
().
entity
(
result
).
build
();
}
}
remote/rest-server/src/main/resources/accountSchema.graphqls
0 → 100644
View file @
b4c805ea
schema {
query: Query
}
type Query {
accounts(id: Int) : [Account]
}
type Account {
id: Int!
owner: String
entries: [AccountEntry]
}
type AccountEntry {
subject: String
value: Int!
date: String
}
\ No newline at end of file
remote/rest-server/src/test/Account service.postman_collection.json
View file @
b4c805ea
{
"info"
:
{
"_postman_id"
:
"
4da4d35a-fef5-417f-96cc-fdd6b4393e2a
"
,
"_postman_id"
:
"
873b504f-ffac-469a-9d38-c1a55744e4a0
"
,
"name"
:
"Account service"
,
"schema"
:
"https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
},
...
...
@@ -11,7 +11,6 @@
{
"listen"
:
"test"
,
"script"
:
{
"id"
:
"675c4b37-800c-4519-accd-5cb9dc0c2827"
,
"exec"
:
[
"pm.test(
\"
Post account
\"
, function () {"
,
" pm.response.to.have.status(201);"
,
...
...
@@ -66,7 +65,6 @@
{
"listen"
:
"test"
,
"script"
:
{
"id"
:
"4fcf2ea9-9496-4db9-8f61-ecd2853d6490"
,
"exec"
:
[
"pm.test(
\"
Get accounts
\"
, function () {"
,
" pm.response.to.have.status(200);"
,
...
...
@@ -87,10 +85,6 @@
"type"
:
"text"
}
],
"body"
:
{
"mode"
:
"raw"
,
"raw"
:
""
},
"url"
:
{
"raw"
:
"http://localhost:8080/accounts"
,
"protocol"
:
"http"
,
...
...
@@ -111,7 +105,6 @@
{
"listen"
:
"test"
,
"script"
:
{
"id"
:
"e9506f16-d8da-4e5d-8dc3-17b0d9560fe5"
,
"exec"
:
[
"pm.test(
\"
Get account
\"
, function () {"
,
" pm.response.to.have.status(200);"
,
...
...
@@ -132,10 +125,6 @@
"type"
:
"text"
}
],
"body"
:
{
"mode"
:
"raw"
,
"raw"
:
""
},
"url"
:
{
"raw"
:
"http://localhost:8080/accounts/:id"
,
"protocol"
:
"http"
,
...
...
@@ -163,7 +152,6 @@
{
"listen"
:
"prerequest"
,
"script"
:
{
"id"
:
"a0021e71-ee2e-460a-b872-eb74e1d4dc61"
,
"exec"
:
[
""
],
...
...
@@ -173,7 +161,6 @@
{
"listen"
:
"test"
,
"script"
:
{
"id"
:
"e61d2ed4-c329-456b-95c0-f30f5889a947"
,
"exec"
:
[
"pm.test(
\"
Patch account
\"
, function () {"
,
" pm.response.to.have.status(200);"
,
...
...
@@ -232,7 +219,6 @@
{
"listen"
:
"test"
,
"script"
:
{
"id"
:
"20b253b3-60bb-43d8-9e26-4bb385253034"
,
"exec"
:
[
"pm.test(
\"
Put account
\"
, function () {"
,
" pm.response.to.have.status(200);"
,
...
...
@@ -291,7 +277,6 @@
{
"listen"
:
"test"
,
"script"
:
{
"id"
:
"a3f15630-0886-4420-a9b7-cd6fe798ca3b"
,
"exec"
:
[
"pm.test(
\"
Delete account
\"
, function () {"
,
" pm.response.to.have.status(204);"
,
...
...
@@ -330,6 +315,43 @@
}
},
"response"
:
[]
},
{
"name"
:
"Get accounts GraphQL"
,
"event"
:
[
{
"listen"
:
"test"
,
"script"
:
{
"exec"
:
[
""
],
"type"
:
"text/javascript"
}
}
],
"protocolProfileBehavior"
:
{
"disableBodyPruning"
:
true
},
"request"
:
{
"method"
:
"GET"
,
"header"
:
[],
"body"
:
{
"mode"
:
"raw"
,
"raw"
:
"{
\r\n
accounts {
\r\n
id
\r\n
owner
\r\n
}
\r\n
}"
},
"url"
:
{
"raw"
:
"http://localhost:8080/accounts_graphql"
,
"protocol"
:
"http"
,
"host"
:
[
"localhost"
],
"port"
:
"8080"
,
"path"
:
[
"accounts_graphql"
]
}
},
"response"
:
[]
}
]
}
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment