我有一个React应用程序,我需要使用Apollo Client向现有REST API服务器发出请求。
一种可能的方法是创建包装现有REST API的GraphQL服务器,但我更愿意在客户端执行此操作并直接调用它。我认为这是过渡时期的临时解决方案。
尽管我们还没有GraphQL服务器,现在转向GraphQL的目的是简化客户端代码并利用Apollo缓存功能来加速应用程序。
感谢。
答案 0 :(得分:2)
这应该可以通过Apollo links来实现。但要注意,因为这是阿波罗的一个积极发展的领域,所以会有粗糙的边缘。
例如,请查看apollo-link-rest。
另见the official links获取灵感&参考,如果您最终想要编写自己的链接以满足您的需求。
答案 1 :(得分:1)
半年后回答我自己的问题。
基本上有两种可能的选择:apollo-link-rest
和apollo-bridge-link
。
我已经分析了两者,并尝试使用它们,并决定使用apollo-bridge-link,尽管它不是由Apollo团队推广的,并且下载和喜欢的次数比apollo-link-rest少得多。我可以说这是正确的决定。我没有困难。
这是apollo-bridge-link
的创建者在apollo-bridge-link
上写的good article和demo app。
您必须创建graphql模式和解析器才能使apollo-link-rest
工作。就像您在graphql服务器上执行此操作一样。然后,如果您决定构建graphql服务器,则在服务器上重用此架构和解析器将非常容易。关于import torch
import numpy, math
import matplotlib.pyplot as plt
nofSequences = 5
maxLength = 130
# Generate training data
x_np = numpy.zeros((nofSequences,maxLength,1))
y_np = numpy.zeros((nofSequences,maxLength))
numpy.random.seed(1)
for i in range(0,nofSequences):
startPos = numpy.random.random()*50
for j in range(0,maxLength):
if j>=startPos and j<startPos+10:
x_np[i,j,0] = math.sin((j-startPos)*math.pi/10)
else:
x_np[i,j,0] = 0.0
if j<startPos+10:
y_np[i,j] = 1
else:
y_np[i,j] = 0
# Define the neural network
INPUT_SIZE = 1
class RNN(torch.nn.Module):
def __init__(self):
super(RNN, self).__init__()
self.rnn = torch.nn.LSTM(
input_size=INPUT_SIZE,
hidden_size=16, # rnn hidden unit
num_layers=1, # number of rnn layer
batch_first=True,
)
self.out = torch.nn.Linear(16, 2)
def forward(self, x, h_state):
r_out, h_state = self.rnn(x, h_state)
outs = [] # save all predictions
for time_step in range(r_out.size(1)): # calculate output for each time step
outs.append(self.out(r_out[:, time_step, :]))
return torch.stack(outs, dim=1), h_state
# Learn the network
rnn = RNN()
optimizer = torch.optim.Adam(rnn.parameters(), lr=0.01)
h_state = None # for initial hidden state
x = torch.Tensor(x_np) # shape (batch, time_step, input_size)
y = torch.Tensor(y_np).long()
torch.manual_seed(2)
numpy.random.seed(2)
for step in range(100):
prediction, h_state = rnn(x, h_state) # rnn output
# !! next step is important !!
h_state = None
loss = torch.nn.CrossEntropyLoss()(prediction.reshape((-1,2)),torch.autograd.Variable(y.reshape((-1,)))) # calculate loss
optimizer.zero_grad() # clear gradients for this training step
loss.backward() # backpropagation, compute gradients
optimizer.step() # apply gradients
errTrain = (prediction.max(2)[1].data != y).float().mean()
print("Error Training:",errTrain.item())
###############################################################################
steps = range(0,maxLength)
plotChoice = 3
plt.figure(1, figsize=(12, 5))
plt.ion() # continuously plot
plt.plot(steps, y_np[plotChoice,:].flatten(), 'r-')
plt.plot(steps, numpy.argmax(prediction.detach().numpy()[plotChoice,:,:],axis=1), 'g-')
plt.plot(steps, x_np[plotChoice,:,0].flatten(), 'b-')
plt.ioff()
plt.show()
,我不能说什么。
他们的方法是避免使用架构使事情变得简单。但是,另一方面,您将不得不编写type patchers。看看测试套件中的example,了解它在现实世界中的应用会变得多么复杂。